2010-12-02 75 views
2

我在使用SortDescription时遇到了问题。我发现了一些关于这个问题的线索,比如如果你想按照一个没有实现IComparable的类型进行排序,就像用户定义的类一样,但这不是我的情况。使用WPF中的SortDescription问题 - int和string不是IComparable?

我有一个类,它有两个属性:字符串ID和int值。我们称之为物品! 我有一个观点:

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

事件处理是这样的:“失败的阵列中来比较两个元素”

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异常,因为它,这是因为两者都不元素实现IComparable。 也就是说,我无法理解,因为我可以比较整数以及字符串。

感谢您的任何想法!

回答

1

这对我来说很好,所以你在代码的其他部分做错了事。比较你做什么和下面的例子。

XAML:

<Window x:Class="SortDemo.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <StackPanel> 
     <Button Click="OnSort" Content="Sort by ID" Tag="ID"/> 
     <Button Click="OnSort" Content="Sort by Value" Tag="Value"/> 
     <ItemsControl Name="_itemsControl" ItemsSource="{Binding Path=Items}" /> 
    </StackPanel> 
</Window> 

后面的代码:

using System; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Windows; 

namespace SortDemo 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      Items = new ObservableCollection<Item>(); 
      Items.Add(new Item() { ID = "AAA", Value = 2 }); 
      Items.Add(new Item() { ID = "BBB", Value = 1 }); 

      DataContext = this; 
     } 

     public ObservableCollection<Item> Items { get; private set; } 

     private void OnSort(object sender, RoutedEventArgs e) 
     { 
      string sortProperty = (sender as FrameworkElement).Tag as string; 
      _itemsControl.Items.SortDescriptions.Clear(); 
      _itemsControl.Items.SortDescriptions.Add(new SortDescription(sortProperty, ListSortDirection.Ascending)); 
     } 
    } 

    public class Item 
    { 
     public string ID { get; set;} 
     public int Value { get; set; } 

     public override string ToString() 
     { 
      return ID + " " + Value; 
     } 
    } 
} 
+0

我是个骗子。我的例子太不精确了。 mItemsControl.Items.SortDescriptions.Add(this.IDDescending);是我用过的,其中IDAscending的getter创建了一个新的SortDescription(“ID”,ListSortDirection.Descending),但正如我发现的那样,通过这种方式得到的值是某种默认的Sortdescription。它的方向是升序,而PropertyName是空的。 (我使用该解决方案能够添加和删除确切的sortdescriptions。) 我不知道为什么,但这个shuld是答案。 – ike 2010-12-03 15:40:38

0

我不知道这是否是相关的,但试图挑选一个科拉姆,这是原因,当我收到一个DataGridView同样的错误:

必须由同一类型的单元格的值为了进行比较。如果您有三行,并且在两行中您具有相同类型的值,但另一行没有值,则如果具有值的两行已设置为对象作为单元格的值,则无法对其进行比较在没有值的行中将是一个空字符串。为了排序,您必须为要排序的列中的所有单元格设置值为字符串值。

0

它在运行时类型在你被排序的列项。属性ID和值必须都是派生IComparable的类型。

我假设你并没有试图做一些聪明的事情,并将所有具有名为ID和值的属性的不同运行时类型放入Items列表中。你可以使用WPF来做类似动态的事情,但这是一个倒下的例子。

String implement IComparable http://msdn.microsoft.com/en-us/library/system.string.aspx