2009-08-28 14 views
8

バインドされたアイテム(ViewModelオブジェクトのリスト)を自動的にソートするための方法はありますか?ItemsControlはアイテムのプロパティの1つに基づいています。 ItemsControlはDataTemplateの一部です。私はCollectionViewSourceがトリックを行うと思ったが、ItemsViewrolにCollectionViewSourceをどのようにバインドするのだろうか? follwoingコードは何もdispaysない:DataTemplateでバインドされたItemsControlをソートする(XAMLのみ)

<--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> 
+0

「雇用」の綴りは間違っていますか?そうでなければ私には大丈夫です。 – Crispy

+0

ViewModelバインディング({x:Type vm:Company})がressourceスコープ内で認識されていないか、評価されていないように見えることはありません。従業員は会社btwの財産です。 – bitbonk

答えて

20

Viewbox直接ではなくDataTemplateの範囲にCollectionViewSourceリソースを移動してみてください:

<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> 
+3

答えの拡張として、その理由は、DataTemplateのルート要素にのみDataContextが設定されているためです。 DataTemplate自体はそうではありません。 DataContextはテンプレート化されたオブジェクトにバインドする唯一の方法であるため、Null以外のDataContextの範囲にリソースを配置する必要があります。 – Gusdor

5

私はこれを行うにする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> 

幸運を指定することで、ソート順を選択することができます!

関連する問題