2016-07-30 3 views
0

TabControl 내부에 표시되는 UserControl 내에서 DataGrid를 정렬하고 있습니다.TabControl에서 새 탭을 선택한 후 CollectionViewSource가 정렬되지 않음

기본 창에는 여기에 표시된 TabControl이 있습니다.

<Grid> 
    <TabControl x:Name="EquipTabs" ItemsSource="{Binding Equipment}" > 
     <TabControl.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Name}" /> 
      </DataTemplate> 
     </TabControl.ItemTemplate> 
     <TabControl.ContentTemplate> 
      <DataTemplate> 
       <ctrls:MachData DataContext="{Binding Path=MachineViewModel}" /> 
      </DataTemplate> 
     </TabControl.ContentTemplate> 
    </TabControl> 
</Grid> 

사용자 정의 컨트롤은 첫 번째 탭이 활성화 될 때 데이터 격자를 올바르게 정렬합니다. 그러나 다른 탭을 클릭하거나 원래 탭으로 다시 전환하면 DataGrid가 정렬되지 않습니다.

<UserControl.Resources> 
    <CollectionViewSource x:Key="StatesSource" Source="{Binding States}" > 
     <CollectionViewSource.SortDescriptions> 
      <scm:SortDescription PropertyName="StartTime" Direction="Descending" /> 
     </CollectionViewSource.SortDescriptions> 
    </CollectionViewSource> 
</UserControl.Resources> 

<Grid> 
    <DataGrid x:Name="dgStates" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
       ItemsSource="{Binding Source={StaticResource StatesSource}}"> 
    </DataGrid> 
</Grid> 

는 다음과 같은 바인딩 쇼 추적 :

Deactivate 
    Replace item at level 0 with {NullDataItem} 
    Activate with root item MachDataViewModel (hash=25379957) 
    At level 0 using cached accessor for MachDataViewModel.States: RuntimePropertyInfo(States) 

왜 정렬은 처음에 발생합니까?

도움 주셔서 감사합니다.

답변

0

이것은 DataGrid 방식으로 인해 발생합니다. DataGridItemsSource으로 설정하기 전에 SortDescriptions을 삭제합니다.

DataGridItemsSource으로 끝나면 SortDescriptions을 적용 할 수있는 경우 SortDescriptions이 적용됩니다.

TargetUpdated 이벤트가 발생하지만이를 사용하려면 NotifyOnTargetUpdated=TrueBinding에 설정해야합니다.

<DataGrid 
    TargetUpdated="dgStates_TargetUpdated" 
    ItemsSource="{Binding Source={StaticResource StatesSource}, NotifyOnTargetUpdated=True}" /> 

코드 :

private void dgStates_TargetUpdated(object sender, DataTransferEventArgs e) 
{ 
    CollectionViewSource sc = this.Resources["StatesSource"] as CollectionViewSource; 
    sc.SortDescriptions.Add(new System.ComponentModel.SortDescription("Name", System.ComponentModel.ListSortDirection.Ascending)); 
}