8

바인딩 된 항목 (ViewModel 개체 목록)을 자동으로 정렬하는 유일한 방법은 항목의 속성 중 하나를 기반으로 ItemsControl입니다. ItemsControl은 DataTemplate의 일부입니다. CollectionViewSource가 트릭을 수행 할 것이라고 생각했지만 CollectionViewSource를 ItemsControl에 바인딩하는 방법은 무엇입니까? 때라도 코드 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

"Employess"의 철자가 잘못되었습니다. 그렇지 않으면 나에게 괜찮아 보인다. – Crispy

+0

아니오 여기에 ViewModel 바인딩 ({x : Type vm : Company})이 ressource 범위 내에서 알려지지 않았거나 평가되지 않은 것 같습니다. 종업원은 회사 btw의 자산입니다. – bitbonk

답변

20

직접 DataTemplateViewbox의 범위에 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이 나 뷰 박스를 사용하지 않았다. 당신은 .... 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> 

행운을 빕니다 정렬 순서를 선택할 수 있습니다!

관련 문제