问题
I have a DataGrid
with a DataGridComboBox
column that works fine. It binds properly to the list of items, correctly displays the selected item, correctly selects an item, and binds the selected item's ID to the underlying row model properly.
It looks like this:
<DataGridComboBoxColumn
x:Name="AssetColumn"
Width="3*"
DisplayMemberPath="Item"
Header="Item"
ItemsSource="{Binding Data.AssetDescriptions, Source={StaticResource proxy}}"
SelectedValueBinding="{Binding AssetDescriptionID}"
SelectedValuePath="AssetDescriptionID" />
I'm trying to achieve the same thing with a DataGridTemplateColumn and an ordinary combo box. My effort:
<DataGridTemplateColumn Header="ItemTemplate" Width="3*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
DisplayMemberPath="Item"
ItemsSource="{Binding Data.AssetDescriptions, Source={StaticResource proxy}}"
SelectedValue="{Binding AssetDescriptionID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="AssetDescriptionID" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Unfortunately, this one doesn't respect the IEditable<T>
interface that the Asset objects implement. Consequently, the usual editing mechanisms that you would normally have in a DataGrid, such as undo, will not work for this column.
Is there a way to write this in such a way that it works the way it's supposed to with the IEditable<T>
interface?
Note: I am aware that I probably need a CellEditingTemplate
to get this to work properly, but I'm not sure how to go about that. I can put the same combo box in the CellEditingTemplate
as well, but I can't figure out how to get the column to switch over to editing mode once it receives the focus, and if I only put the combo box data template in the CellEditingTemplate
, no data is displayed in the column.
来源:https://stackoverflow.com/questions/53549281/how-do-i-get-a-datagridtemplatecolumn-to-work-with-objects-implementing-the-iedi