Caliburn.Micro使用ItemsControl查看模型的绑定列表
问题描述:
我有一个包含另一个视图模型集合的视图模型。我想使用视图模型集合作为ItemsControl
的ItemsSource
来显示相应的视图。我收到这个错误:items collection must be empty before using itemssource
。每当CurrentArea
更改时,我想创建新的视图模型。Caliburn.Micro使用ItemsControl查看模型的绑定列表
父视图模型
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));
}
}
父视图
<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>
视图模型用于收集
public class WeaponDetailViewModel : PropertyChangedBase
{
// NOTHING SPECIAL HERE
}
查看对应于查看用于收集
<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>
答
第一关模式,你应该阅读CodePlex上(https://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Conventions)的文档。
不过我相信你的itemcontrol的内部元素声明是错误的。
换句话说删除
<ContentControl cal:View.Model="{Binding /}" />
而在你的ItemsControl
<DataTemplate>
<ContentControl cal:View.Model="{Binding}" />
</DataTemplate>
这是正确的创建项目的itemstemplate。我在''里添加了一个'',它不再抱怨'ItemsSource'。我现在正在收到关于无法为视图模型找到视图的错误!有时间做更多的研究。 –
Jeff
如果我是你,我会得到源代码,将其添加到你的项目参考中,并查看哪些caliburn正在搜索你的视图的命名约定。如果您需要添加新的命名约定,那会给您一个提示。 – TYY