Bacon
Bacon

Reputation: 823

Caliburn.Micro Binding List Of View Models With ItemsControl

I have a view model that contains a collection of another view model. I want to use the collection of view models as the ItemsSource of an ItemsControl to display the corresponding view. I'm getting this error: items collection must be empty before using itemssource. Every time the CurrentArea changes I want to create new view models.

Parent View Model

public class AreaDetailViewModel : Screen
{
    public AreaDetailViewModel()
    {
        CurrentAreaWeapons = new ObservableCollection<WeaponDetailViewModel>();
        DisplayName = "Area Details";

        using (var repo = new AreaDetailRepository())
            Areas = repo.GetAreas();
    }

    public List<Area> Areas;
    public Area CurrentArea
    {
        get { return _currentArea; }
        set
        {
            _currentArea = value;
            DisplayName = _currentArea.Name;
            NotifyOfPropertyChange(() => CurrentArea);
            SetWeaponDetailViewModels();
        }
    }

    private Area _currentArea;

    private void SetWeaponDetailViewModels()
    {
        CurrentAreaWeapons.Clear();
        foreach (var item in _currentArea.Weapons)
            CurrentAreaWeapons.Add(new WeaponDetailViewModel(item));
    }
}

Parent View

<UserControl x:Class="Fallout4Checklist.Views.AreaDetailView"
             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:cal="http://www.caliburnproject.org">

    <ScrollViewer>
        <StackPanel>
            <Border Style="{StaticResource WeaponContainer}">
                <StackPanel>
                    <Label Style="{StaticResource WeaponHeader}" />
                    <ItemsControl ItemsSource="{Binding CurrentAreaWeapons}">
                        <ContentControl cal:View.Model="{Binding /}" />
                    </ItemsControl>
                </StackPanel>
            </Border>
        </StackPanel>
    </ScrollViewer>
</UserControl>

View Model Used In Collection

public class WeaponDetailViewModel : PropertyChangedBase
{
    // NOTHING SPECIAL HERE
}

View That Corresponds To View Model Used In Collection

<UserControl x:Class="Fallout4Checklist.Views.Partial.WeaponDetail"
             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:Fallout4Checklist.Views.Partial">

    <StackPanel>
        <Border Style="{StaticResource WeaponDetailNameBorder}">
            <Label Style="{StaticResource WeaponDetailName}" />
        </Border>
        <StackPanel Style="{StaticResource WeaponDetailContainer}">
            <Border Style="{StaticResource ImageBorder}">
                <Image Source="{Binding ImagePath.FullPath}" />
            </Border>
            <Border Style="{StaticResource ItemDescriptionBorder}">
                <TextBlock Style="{StaticResource ItemDescription}" Text="{Binding Characteristics}" />
            </Border>
        </StackPanel>
    </StackPanel>
</UserControl>

Upvotes: 2

Views: 4602

Answers (1)

TYY
TYY

Reputation: 2716

First off you should read the documentation on codeplex (https://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Conventions).

Offhand however I believe your itemcontrol's inner element declaration is wrong.

In other words remove

<ContentControl cal:View.Model="{Binding /}" />

And create an itemstemplate for the items in your ItemsControl

<DataTemplate>
    <ContentControl cal:View.Model="{Binding}" />
</DataTemplate>

Upvotes: 5

Related Questions