I’m writing an MVVM WPF app with a datagrid of values which needs to be editable. The meaning of the value varies depending on other data, so I’ve written a usercontrol for editing them with a template that varies depending on the type of value. i.e. it may appear as a textbox, a combobox, or one of several other inhouse controls that link to databases to retrieve possible values.
This is the xaml I’ve used.
<DataGridTemplateColumn Header="Value">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:ValueViewingControl Value="{Binding Value}" ValueType="{Binding SettingValueType}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<local:ValueEditingControl Value="{Binding Value,Mode=TwoWay}" ValueType="{Binding SettingValueType}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
The problem is that once in editing mode, the next click on any part of the ValueEditingControl causes the cell to exit edit mode and go back to the ValueViewingControl before I can actually do anything.
I assume its something to do with the cell thinking its lost focus. Does anyone know a way of keeping the cell in edit mode until I actually tab or click out of the cell?
[edit]
A little more testing shows me that it works as expected if I use a textbox or a standard combobox as the edit control. This is making me think its the implementation of the custom controls I’ve written, which incorporate popups. When I select the popup that is part of the editing control, it thinks I’ve left the datagridcell and so ends editmode. How do I convince the datagridcell that the popup is inside it for focus purposes?
It must be possible or the combobox wouldnt work.
Thanks to this question WPF Popup focus in data grid , I’ve figured out a partial solution to my own problem.
The problem seems to be that the datagridcell over which my popups are floating is trying to snatch the focus when I click inside my popups.
My solution is to add this handler and helper function to the usercontrol containing the popup
This stops the focus from leaving if the popup is open. It’s not perfect, so if anyone has a better solution I’m all ears, but it works.