I have a DataGrid, with only one column (for the sake of this example). This column is a 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>
The DataGrid's ItemsSource is set to a List:
public class InsertableRecord
{
public int RoomCode { get; set; }
}
The ComboBox's ItemsSource in the DataGridTemplateColumn is bound to a property in my Window:
public List<Room> AllRooms
{
get;
private set;
}
Here is the definition of the class "Room":
public partial class Room
{
public string ID { get; set; }
public string Description { get; set; }
public string DisplayText
{
get
{
return this.ID + " (" + this.Description + ")";
}
}
}
Notice that I have my SortMemberPath set to DisplayText, which is a property of "Room", and not of "InsertableRecord". So obviously, I get a binding error when I try to sort this column saying the property "DisplayText" does not exist in the object "InsertableRecord".
How would I go about sorting the column based on the current Text of the ComboBox (or the DisplayText property of the "Room" object, both would work)?