【问题标题】:sorting a bound ItemsControl in a DataTemplate (XAML only)在 DataTemplate 中对绑定的 ItemsControl 进行排序(仅限 XAML)
【发布时间】: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


【解决方案1】:

尝试将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>

【讨论】:

  • 作为答案的扩展,原因是因为只有 DataTemplate 的根元素具有它的 DataContext 集。 DataTemplate 本身没有。由于 DataContext 是绑定到模板化对象的唯一方法,因此您必须将资源置于非 Null DataContext 的范围内。
【解决方案2】:

我没有使用 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>

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 2015-03-25
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多