是否有一种 XAML 唯一方法可以根据项目的属性之一自动对绑定(bind)项目(ViewModel 对象列表)ItemsControl 进行排序。 ItemsControl 是 DataTemplate 的一部分。我认为 CollectionViewSource 可以解决问题,但是如何将 CollectionViewSource 绑定(bind)到 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>
最佳答案
尝试移动 CollectionViewSource
Viewbox
范围内的资源而不是直接 DataTemplate
:
<DataTemplate DataType="{x:Type vm:Company}">
<Viewbox>
<Viewbox.Resources>
<CollectionViewSource x:Key="viewSource" Source="{Binding Employees}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="ID" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Viewbox.Resources>
<ItemsControl ItemsSource="{Binding Source={StaticResource viewSource}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Viewbox>
</DataTemplate>
关于wpf - 在 DataTemplate 中对绑定(bind)的 ItemsControl 进行排序(仅限 XAML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1348028/