8

Esiste un modo XAML per ordinare automaticamente gli elementi associati (elenco dell'oggetto ViewModel) ItemsControl in base a una delle proprietà degli elementi. ItemsControl è parte di un DataTemplate. Pensavo che CollectionViewSource avrebbe fatto il trucco, ma come legare CollectionViewSource a ItemsControl. Il codice dispays follwoing nulla:ordinamento di un oggetto ItemsControl in un DataTemplate (solo 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

Qualsiasi cosa abbia a che fare con "Dipendente" che viene digitata erroneamente? Altrimenti mi sembra a posto. – Crispy

+0

Non è che il problema qui sembra essere (forse) che l'associazione ViewModel ({x: Type vm: Company}) non è nota o non è valutata all'interno dell'ambito di lavoro. I dipendenti sono una proprietà della compagnia btw. – bitbonk

risposta

20

Provare a spostare la risorsa CollectionViewSource al campo di applicazione della Viewbox piuttosto che direttamente alla 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> 
+3

Come estensione della risposta, il motivo è perché solo l'elemento radice di un DataTemplate ha il set DataContext. Lo stesso DataTemplate non lo fa. Dato che DataContext è l'unico modo per associare all'oggetto basato su modello, è necessario inserire la risorsa nell'ambito di un DataContext non Null. – Gusdor

5

io non utilizzare un DataTemplate o viewBox per fare questo. È possibile scegliere il tipo di ordinamento specificando un'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> 

Buona fortuna!