我有一个 DataGrid,只有一列(为了这个例子)。此列是 DataGridTemplateColumn:
<DataGrid x:Name="grdMainGrid">
<DataGridTemplateColumn Header="Room" CanUserSort="True" SortMemberPath="DisplayText" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=AllRooms, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" Height="20" SelectedValuePath="Code" SelectedValue="{Binding Path=RoomCode, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="DisplayText" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
DataGrid 的 ItemsSource 设置为一个列表:
public class InsertableRecord
{
public int RoomCode { get; set; }
}
DataGridTemplateColumn 中的 ComboBox 的 ItemsSource 绑定到我的窗口中的一个属性:
public List<Room> AllRooms
{
get;
private set;
}
这是“房间”类的定义:
public partial class Room
{
public string ID { get; set; }
public string Description { get; set; }
public string DisplayText
{
get
{
return this.ID + " (" + this.Description + ")";
}
}
}
请注意,我将 SortMemberPath 设置为 DisplayText,它是“Room”的属性,而不是“InsertableRecord”的属性。很明显,当我尝试对该列进行排序时说属性“DisplayText”在对象“InsertableRecord”中不存在时出现绑定错误。
我将如何根据组合框的当前文本(或“房间”对象的 DisplayText 属性,两者都可以)对列进行排序?