在DataTemplate中排序绑定的ItemsControl(仅限XAML)

bit*_*onk 10 sorting wpf datatemplate itemscontrol collectionviewsource

是否有一种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>
Run Code Online (Sandbox Code Playgroud)

Ken*_*art 22

尝试将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>
Run Code Online (Sandbox Code Playgroud)

  • 作为答案的扩展,原因是因为只有DataTemplate的根元素才有它的DataContext集.DataTemplate本身没有.由于DataContext是绑定到模板化对象的唯一方法,因此必须将资源放在非空数据文本的范围内. (3认同)

cra*_*ond 10

我没有使用DataTemplate或ViewBox来执行此操作.您可以通过指定ItemsControl.Resource来选择排序顺序....

  <ItemsControl xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
       x:Name="MyItemsControl" Loaded="MyItemsControl_Loaded">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <ItemsControl>
        <ItemsControl.Resources>
          <CollectionViewSource x:Key="Orders" Source="{Binding Orders}">
            <CollectionViewSource.SortDescriptions>
              <scm:SortDescription PropertyName="OrderID" Direction="Ascending"/>
            </CollectionViewSource.SortDescriptions>
          </CollectionViewSource>
        </ItemsControl.Resources>
        <ItemsControl.ItemsSource>
          <Binding Source="{StaticResource Orders}"/>
        </ItemsControl.ItemsSource>
        <ItemsControl.ItemsPanel>
          <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
          </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
          <DataTemplate>
            <TextBlock Text="{Binding OrderID}"/>
          </DataTemplate>
        </ItemsControl.ItemTemplate>
      </ItemsControl>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

祝好运!