SortDescription 사용에 문제가 있습니다. 사용자 정의 클래스와 같은 IComparable을 구현하지 않는 형식으로 정렬하려는 경우처럼 문제에 대한 일부 스레드를 찾았습니다.하지만 제 경우는 아닙니다.WPF에서 SortDescription을 사용할 때의 문제 - int와 string은 IComparable이 아닙니까?
클래스에는 두 가지 속성 (문자열 ID 및 int 값)이 있습니다. 그것을 Item이라고 부르 자! 이 때문에
private void Button_Click(object sender, RoutedEventArgs e)
{
mItemsControl.Items.SortDescriptions.Add(new SortDescription("ID", ListSortDirection.Ascending); //Exception here
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
mItemsControl.Items.SortDescriptions.Add(new SortDescription("Value", ListSortDirection.Ascending); //...and here as well
}
내가 InvalidOperationException이 얻을, 그것은의도 때문이다 "배열의 두 요소를 비교하지 못했습니다."
<UserControl> <!-- ... -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Click="Button_Click"
Content="Sort by ID"
Grid.Row="0"/>
<Button Click="Button_Click1"
Content="Sort by Value"
Grid.Row="1"/>
<DockPanel Grid.Row="2">
<ItemsControl x:Name="mItemsControl"
ItemsSource="{Binding Items}"><!-- The type of Items is ObservableCollection<Item> -->
<!-- ... -->
</ItemsControl>
</DockPanel>
</Grid>
</GroupBox>
EventHandlers 다음과 같다 : 는 그리고 전망을 요소는 IComparable을 구현합니다. 그리고, 내가 이해할 수없는 것은, int와 문자열을 비교할 수 있기 때문입니다.
아무쪼록 감사드립니다.
나는 거짓말 쟁이입니다. 나는 내 모범을 너무 부정확했다. mItemsControl.Items.SortDescriptions.Add (this.IDDescending); IDAscending의 getter가 새로운 SortDescription ("ID", ListSortDirection.Descending)을 만드는 곳에서 사용했지만,이 방법으로 얻은 값은 일종의 기본 Sortdescription입니다. 방향이 오름차순이고 PropertyName이 null입니다. (정확한 정렬 설명을 추가하고 제거 할 수있는 솔루션을 사용했습니다.) 이유는 모르겠지만이 응답이 답글입니다. – ike