How do I get a DataGridTemplateColumn to work with objects implementing the IEditable<T> interface?

让人想犯罪 __ 提交于 2019-12-25 03:27:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!