WPF CellEditingTemplate and duplicated events

核能气质少年 提交于 2019-12-11 04:54:43

问题


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?

来源:https://stackoverflow.com/questions/43585684/wpf-celleditingtemplate-and-duplicated-events

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