Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting a bound ItemsControl in a DataTemplate (XAML only)

Is there a XAML only way to automatically sort the bound items (list of ViewModel object) ItemsControl based on one of the properties of the items. The ItemsControl is part of a DataTemplate. I thought CollectionViewSource would do the trick, but how do I bind the CollectionViewSource to the ItemsControl. The follwoing code dispays nothing:

<--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>
like image 991
bitbonk Avatar asked Aug 28 '09 16:08

bitbonk


2 Answers

Try moving the CollectionViewSource resource to the scope of the Viewbox rather than directly the 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>
like image 61
Kent Boogaart Avatar answered Sep 23 '22 15:09

Kent Boogaart


I didn't use a DataTemplate or ViewBox to do this. You can choose the sort order by specifying an 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>

Good luck!

like image 10
crazyDiamond Avatar answered Sep 19 '22 15:09

crazyDiamond