I have a DataGridComboBoxColumn which is bind to a collection of objects which is type is MyItem. MyItem has two string properties: Description and Shortcut.
If the column is not in edit mode I want to show the string from the Shortcut Property and if the column is in edit mode I want to show the string from the Description property.
Is this possible without a DataGridTemplateColumn?
Further information: At the moment I've set the DisplayMemberPath property to "Description".
The best way to do this, I have found, would be to use a DataGridTemplateColumn
instead of a DataGridComboBoxColumn
.
DataGridComboBoxColumn
does not expose either a CellTemplate
(displayed when not editing) nor a CellEditingTemplate
(displayed when editing), and instead builds the ComboBox
templates for you based on the bindings you hand the column. Since you want these to be different, you need a column which exposes both these members, which is DataGridTemplateColumn
.
Simply make the CellTemplate
a Label
bound to your Shortcut
Property, and the CellEditingTemplate
a ComboBox
with the same bindings as those you gave your DataGridComboBoxColumn
.
After all that, your column should look something like this
<DataGridTemplateColumn Header="...">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Shortcut}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="..."
DisplayMemberPath="Description"
SelectedItem="..."/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Addendum - The reason I advocate use of Label
over TextBlock
here is because Label
automatically includes alignment and margins. TextBlock
, lacking these, looks very strange and needs some massaging to get looking right.