Home > Back-end >  How to mark a WPF datagrid column with a custom user control as dirty after editing
How to mark a WPF datagrid column with a custom user control as dirty after editing

Time:09-16

I have a datagrid in WPF and I am using a custom multi select usercontrol in one of the columns like this:

<DataGridTemplateColumn 
    x:Name="APIColumnMulti"
    Width="auto"
    Header="Wirkstoff"
    >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <usercontrols:MultiSelectCell
                x:Name="multiSelect1"
                Title="Wirkstoffe"
                ChosenItems="{Binding ActiveIngredients, UpdateSourceTrigger=PropertyChanged}"
                IsTemplateRow="{Binding IsTemplate, UpdateSourceTrigger=PropertyChanged}"
                ItemsSource="{Binding AllActiveIngredientsList, RelativeSource={RelativeSource AncestorType=UserControl}, UpdateSourceTrigger=PropertyChanged}"
                TemplateName="TemplateProduct.ActiveIngredientList"
                TemplateProduct="{Binding Source.Template, UpdateSourceTrigger=PropertyChanged}" />

        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Almost everything works fine. But when I end editing my multiselect the datagrid row itself is not notified of the change. So when I leave the row without editing for example a standard text field, the contents are not saved as they should be.

How do you mark the row as edited/dirty from a CellTemplate's perspective?

... Still no answer, so maybe I wa not clear enough.

My MultiSelect has a button in it, a big , and if you click it, a dialog opens and you can select the items. Then you click "accept", the dialog is closed and the chosen items are displayed in the cell template (in the usercontrol). But the datagrid does not get notified that the cell has been changed. So when I leave the row, the RowEditEnding event isn't triggered.

What I have found out so far:

  • You need/should implement IEditableObject in the UserControl when you want to use it as an input in the datagrid. But there aren't any events that bubble up to the datagrid, so this seems not to be part of the solution.
  • When I click the cell with the items and the plus button and MISS the button, the parent datagrid realizes that an edit has begun. But when I click directly on the button, the datagrid is not aware of this. I tried to propagate the RoutedEvent from my ClickOnPlusButton delegate, but in vain - obviously I don't know how to do it properly.

I am quite lost.

CodePudding user response:

Ok, finally I found a solution.

In my UserControl I had to go way up the visual tree to get hold of the DataGrid instance. Then a dataGrid.BeginEdit() and a dataGrid.CommitEdit() did the magic and informed the DataGrid that the currently edited row has been changed. This way the DataGrid calls EndEdit() on the ViewModel (which needs the IEditableObject interface) when leaving the row or pressing Enter.

  • Related