У меня есть DataGrid с одним столбцом (для этого примера). Этот столбец является DataGridTemplateColumn:Сортировка DataGridTemplateColumn с ComboBox
<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>
ItemsSource В DataGrid имеет значение в списке:
public class InsertableRecord
{
public int RoomCode { get; set; }
}
ItemsSource В ComboBox находится в DataGridTemplateColumn связан со свойством моего окна:
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, который является свойством «Комната», а не «InsertableRecord». Поэтому, очевидно, я получаю ошибку привязки, когда пытаюсь сортировать этот столбец, говоря, что свойство «DisplayText» не существует в объекте «InsertableRecord».
Как бы я мог сортировать столбец на основе текущего текста ComboBox (или свойства DisplayText объекта «Комната», оба будут работать)?