Caliburn-Micro / Caliburn.Micro

A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
http://caliburnmicro.com/
MIT License
2.8k stars 778 forks source link

Cannot find view for ViewModels when I am using cal:View.Model and cal:View.Context #744

Closed ghost closed 3 years ago

ghost commented 3 years ago

HomeView.XAML

<UserControl x:Class="StrimoUI.Views.Content.HomeView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:StrimoUI.Views.Content"
             xmlns:cal="http://www.caliburnproject.org"
             mc:Ignorable="d" d:DesignWidth="1920" d:DesignHeight="1080">
    <Grid>
       <!-- Unnecessary Part is removed -->

            <Grid Grid.Row="1" Background="Transparent">
                <ItemsControl x:Name="NavigationItemsControl" ItemsSource="{Binding NavigationMenuItems}" VerticalAlignment="Center"
                              ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="Transparent" BorderBrush="Transparent">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <ContentControl cal:View.Context="NavigationItemView" cal:View.Model="{Binding}" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Grid>

        </Grid>
    </Grid>
</UserControl>

HomeViewModel

public class HomeViewModel : Screen
    {
        private readonly IEventAggregator eventAggregator;

        public IEnumerable<NavigationItemViewModel> NavigationMenuItems { get; set; }

        public HomeViewModel(IEventAggregator _eventAggregator)
        {
            eventAggregator = _eventAggregator;

            List<SubItemModel> liveSubItems = new List<SubItemModel>();
            liveSubItems.Add(new SubItemModel("Romanina TV", new UserControl()));
            liveSubItems.Add(new SubItemModel("United Kingdom", new UserControl()));

            NavigationMenuItems = Enumerable.Range(1, 10).Select(x => new NavigationItemViewModel { Header = "Home", ImageName = "home.png", SubItems = liveSubItems, Screen = new UserControl() });
            //NavigationMenuItems.Add(new NavigationItemViewModel() { Header = "Home", ImageName = "home.png", SubItems = liveSubItems, Screen = new UserControl() });

        }
    }

NavigationItemView.XAML

<UserControl x:Class="StrimoUI.Views.Content.NavigationItemView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:StrimoUI.Views.Content"
             mc:Ignorable="d" >
    <Grid>
        <Image Source="{Binding ImageName}" Width="25" Height="22" Margin="0 0 0 22" />
        <ListBoxItem x:Name="ListViewItemMenu" Content="{Binding Path=Header}" Padding="37 14" FontSize="15" Foreground="White" />
        <Expander x:Name="ExpanderMenu" Header="{Binding Path=Header}" IsExpanded="False" Width="210" HorizontalAlignment="Right" Background="{x:Null}" Foreground="White">
            <ListView x:Name="ListViewMenu" ItemsSource="{Binding Path=SubItems}" Foreground="White" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}" Padding="20 5" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Expander>    
    </Grid>
</UserControl>

NavigationItemViewModel

    public class NavigationItemViewModel:PropertyChangedBase
    {
        public string Header { get; set; }
        public string ImageName { get; set; }
        public List<SubItemModel> SubItems { get; set; }
        public UserControl Screen { get; set; }
    }

Cannot find view for StrimoUI.ViewModels.Content.NavigationItemViewModel There is no bugs to run application but this shows in ContentControl. Please solve this problem.

mvermef commented 3 years ago

Double check all namespaces match exactly for the item, ie ViewModels for ViewModels and Views for Views or what ever your schema is for naming

ghost commented 3 years ago

All namespaces are correct for me. I think Caliburn has the bug for this problem. I just changed cal:View.Context="MenuItem" from cal:View.Context="NavigationItemView" This is working even if ViewModel is NavigationItemViewModel. Very strange

ghost commented 3 years ago

I just removed cal:View.Context="MenuItem". This is working with NavigationItemView automatically. I just figured it out. cal:View.Context will work only when I want to show 2 views in one viewmodel. Thank you for reply. @mvermef