[英]silverlight datagrid: sorting column bound to an object
I have datagrid bound to an ObservableCollection. 我有绑定到ObservableCollection的数据网格。 One of the colum is bound to the object itself rather then one the properties:
列之一绑定到对象本身,而不是属性:
<sdk:DataGrid
ItemsSource="{Binding PersonList}"
AutoGenerateColumns="False">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn
Binding="{Binding}"
Header="Person"
SortMemberPath="FirstName"
/>
<sdk:DataGridTextColumn
Binding="{Binding FirstName }"
Header="FirstName"
/>
<sdk:DataGridTextColumn
Binding="{Binding LastName }"
Header="LastName"
/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
I want to make the column bound to the object itself sortable. 我想使绑定到对象本身的列可排序。 I thought it would have been enough to make the class Person implement IComparable.
我认为让类Person实现IComparable就足够了。 But it seems this is not enough:
但这似乎还不够:
public class Person : IComparable<Person>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return string.Format("{0} {1}", FirstName, LastName);
}
public int CompareTo(Person other)
{
return FirstName.CompareTo(other.FirstName);
}
}
Try this one, you dont have to create separate column for sorting, 试试这个,您不必创建单独的列进行排序,
<sdk:DataGrid
ItemsSource="{Binding PersonList}"
AutoGenerateColumns="False">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn
Binding="{Binding FirstName }"
Header="FirstName"
CanUserSort="True"
/>
<sdk:DataGridTextColumn
Binding="{Binding LastName }"
Header="LastName"
/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.