WPF CellEditingTemplate and duplicated events

165 Views Asked by At

I've a WPF DataGrid with a DataGridTemplateColumn like this:

<DataGridTemplateColumn IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock
            Text="{Binding Path=MyProperty, Mode=OneWay}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <TextBox
            Text="{Binding Path=MyProperty, UpdateSourceTrigger=PropertyChanged}"
            TextChanged="ctl_TextChanged" />
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

I noticed that every time I go into a cell edit, a new textbox control is generated and, consequently, if I start typing a character, the TextChanged event is invoked several times...once for each instance of the control that was generated!

Sample project to reproduce the issue: TestEditingTemplate_4.5.2

I used the TextChanged event only for example, but the issue may also occur with other events (eg. events defined within a UserControl)

Is there is a way to avoid this behavior? I wish to destroy the "edit" control on CellEditEnding, so that it does not interfere with the new control generated when I return to cell edit; how can I do?

0

There are 0 best solutions below