2010-02-16 36 views
2

myListBox.Items.SortDescriptions.Add( new SortDescription(「BoolProperty」,ListSortDirection.Descending));ICollectionView.SortDescriptions不適用於布爾值

此排序僅適用於基礎項目的字符串屬性。不是用布爾值?這是有原因的嗎?

謝謝!

UPDATE:

是的,你的例子確實有效。但是我的例子有什麼問題?

public class A 
{ 
    public bool Prop;    
} 

List<A> l = new List<A>() { 
    new A() { Prop = true }, 
    new A() { Prop = false }, 
    new A() { Prop = true }, 
}; 

ICollectionView icw = CollectionViewSource.GetDefaultView(l);             
icw.SortDescriptions.Add(new SortDescription("Prop", ListSortDirection.Ascending));     
icw.Refresh(); 

回答

3

嗯,我似乎在我的列表示例中的布爾屬性添加SortDescription!背後

<Window x:Class="WpfApplication3.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"> 
    <Grid> 
     <ListBox x:Name="box" DisplayMemberPath="Test" /> 
    </Grid> 
</Window> 

代碼:

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

     //4 instances, each with a property Test of another boolean value 
     box.ItemsSource = new[] { 
      new {Test = true}, 
      new {Test = false}, 
      new {Test = false}, 
      new {Test = true} 
     }; 

     box.Items.SortDescriptions.Add(new SortDescription("Test", ListSortDirection.Descending)); 
    } 
} 


public class BooleanHolder 
{ 
    public bool Test { get; set; } 
} 

就像一個魅力;)

也許你拼錯在SortDescription對象的屬性名字嗎?希望這有助於

在您的示例中,您將Prop定義爲字段。使它成爲一個財產,它會工作;)

public class A 
{ 
    public bool Prop { get; set; } 
} 
+0

是的,你的例子真的有用,但看看我的請,我沒有看到巨大的差異。 –

+0

支柱不是一個財產,但是一個領域..存在差異! ;) WPF尋找屬性,並沒有找到它! – Arcturus

+0

是的,我意識到了它的區別。謝謝你,這個領域應該是私人的..也許這就是原因 –