我试图在嵌套在另一个CollectionViewSource
内的ItemsControl
中使用ItemsControl
。
我没有XAML警告/错误,但不显示数据。
如果我将ItemsSource
属性直接绑定到ObservableCollection
,则显示项目没有问题。
我的ViewModel基本上是一个嵌套在另一个集合中的集合。
XAML
<UserControl x:Class="View"
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:v="clr-namespace:ViewModel"
xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<TabControl ItemsSource="{Binding Categories}" SelectedItem="{Binding SelectedCategory, Mode=TwoWay}">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate DataType="v:CategoryViewModel">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.ItemContainerStyle>
<TabControl.ContentTemplate>
<DataTemplate DataType="v:CategoryViewModel">
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Groups}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<CollectionViewSource x:Key="ValuesCollection" Source="{Binding Values}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</DataTemplate.Resources>
<GroupBox Header="{Binding Name}" >
<ItemsControl ItemsSource="{Binding Source={StaticResource ValuesCollection}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="10,2,10,2">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Name}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Value}""/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</GroupBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</UserControl>
CategoryViewModel
namespace ViewModel
{
using System.Collections.ObjectModel;
using System.Diagnostics;
internal class CategoryViewModel : ObservableObjectBase
{
private string name;
private string id;
private int priority;
public CategoryViewModel()
{
this.Groups = new ObservableCollection<GroupViewModel>();
}
public ObservableCollection<GroupViewModel> Groups { get; }
public string Name
{
get
{
return this.name;
}
set
{
this.SetValue(ref this.name, value);
}
}
public string Id
{
get
{
return this.id;
}
set
{
this.SetValue(ref this.id, value);
}
}
public int Priority
{
get
{
return this.priority;
}
set
{
this.SetValue(ref this.priority, value);
}
}
}
}
GroupViewModel
namespace ViewModel
{
using System.Collections.ObjectModel;
using System.Diagnostics;
internal class GroupViewModel : ObservableObjectBase
{
private string name;
private string id;
private int priority;
public GroupViewModel()
{
this.Values = new ObservableCollection<ValueViewModel>();
}
public ObservableCollection<ValueViewModel> Values { get; }
public string Name
{
get
{
return this.name;
}
set
{
this.SetValue(ref this.name, value);
}
}
public string Id
{
get
{
return this.id;
}
set
{
this.SetValue(ref this.id, value);
}
}
public int Priority
{
get
{
return this.priority;
}
set
{
this.SetValue(ref this.priority, value);
}
}
}
}
ValueViewModel
namespace ViewModel
{
using System.Diagnostics;
internal class ValueViewModel : ObservableObjectBase
{
private string name;
private string id;
private string value;
private int priority;
public string Name
{
get
{
return this.name;
}
set
{
this.SetValue(ref this.name, value);
}
}
public string Id
{
get
{
return this.id;
}
set
{
this.SetValue(ref this.id, value);
}
}
public string Value
{
get
{
return this.value;
}
set
{
this.SetValue(ref this.value, value);
}
}
public int Priority
{
get
{
return this.priority;
}
set
{
this.SetValue(ref this.priority, value);
}
}
}
}
有什么建议吗?
提前致谢。
修改 在输出中我看到:
System.Windows.Data错误:2:找不到管理FrameworkElement 或目标元素的FrameworkContentElement。 BindingExpression:路径=值;的DataItem = NULL;目标元素是 &#39; CollectionViewSource&#39; (的HashCode = 40230093);目标属性是 &#39;来源&#39; (键入&#39;对象&#39;)
答案 0 :(得分:1)
不应将CollectionViewSource
放在DataTemplate.Resources
中,而应将其放在数据模板中内部的资源中,例如<GroupBox.Resources>
< / p>