我有一个绑定到ObservableCollection的datagrid。其中一个列绑定到对象本身而不是一个属性:
<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>
我想让绑定到对象本身的列可以排序。我认为让Person类实现IComparable已经足够了。但似乎这还不够:
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);
}
}
答案 0 :(得分:0)
试试这个,你不必创建单独的列进行排序,
<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>