【发布时间】:2009-08-28 16:14:25
【问题描述】:
是否有一种 XAML 唯一方法可以根据项目的属性之一自动对绑定项目(ViewModel 对象列表)ItemsControl 进行排序。 ItemsControl 是 DataTemplate 的一部分。我认为 CollectionViewSource 可以解决问题,但是如何将 CollectionViewSource 绑定到 ItemsControl。以下代码什么也没显示:
<--xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"-->
<DataTemplate DataType="{x:Type vm:Company}">
<DataTemplate.Resources>
<CollectionViewSource x:Key="viewSource" Source="{Binding Employees}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="ID" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</DataTemplate.Resources>
<Viewbox>
<ItemsControl ItemsSource="{Binding Source={StaticResource viewSource}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Viewbox>
</DataTemplate>
【问题讨论】:
-
“Employess”拼写错误有什么关系吗?否则我觉得没问题。
-
不,这里的问题似乎(可能)是 ViewModel 绑定 ({x:Type vm:Company}) 在资源范围内未知或未评估。员工是公司的财产。
标签: wpf sorting datatemplate itemscontrol collectionviewsource