Bac*_*con 2 .net c# wpf xaml caliburn.micro
我有一个视图模型,其中包含另一个视图模型的集合。我想使用视图模型的集合作为显示相应视图ItemsSource的。ItemsControl我收到此错误:items collection must be empty before using itemssource。每次CurrentArea更改时我都想创建新的视图模型。
父视图模型
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));
}
}
Run Code Online (Sandbox Code Playgroud)
父视图
<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>
Run Code Online (Sandbox Code Playgroud)
查看集合中使用的模型
public class WeaponDetailViewModel : PropertyChangedBase
{
// NOTHING SPECIAL HERE
}
Run Code Online (Sandbox Code Playgroud)
与集合中使用的视图模型相对应的视图
<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>
Run Code Online (Sandbox Code Playgroud)
首先,您应该阅读 codeplex 上的文档(https://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Conventions)。
不过,我相信你的 itemcontrol 的内部元素声明是错误的。
换句话说,删除
<ContentControl cal:View.Model="{Binding /}" />
Run Code Online (Sandbox Code Playgroud)
并为 ItemsControl 中的项目创建一个项目模板
<DataTemplate>
<ContentControl cal:View.Model="{Binding}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4562 次 |
| 最近记录: |