本文介绍了WPF CellEditingTemplate和重复事件,如何避免呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有DataGridTemplateColumn的WPF DataGrid这个:

<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>

我注意到,每次进行单元格编辑时,都会生成一个新的文本框控件,因此,如果我开始键入一个字符,则将多次调用TextChanged事件……对于该控件的每个实例,生成了!

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!

重现此问题的示例项目: TestEditingTemplate_4.5.2

Sample project to reproduce the issue: TestEditingTemplate_4.5.2

我仅使用了TextChanged事件,但是其他事件(例如,在UserControl中定义的事件)也可能发生此问题,有没有办法避免这种行为?我希望销毁编辑"控件上的CellEditEnding,这样 当我返回单元格编辑时,它不会干扰生成的新控件;

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?


推荐答案

>> TextChanged事件将被调用多次...针对生成的控件的每个实例一次!

我下载了您的演示并进行了测试.它将触发两次TextChanged.

I download your demo and make test on my side. It will fire the TextChanged twice.

TextChanged时将触发以下代码.由于您的TextBox绑定模式是默认的(TwoWay),因此在触发MyProperty后,它将再次触发TextChanged.

The following code will be triggered when TextChanged. Because your TextBox bind mode is the default(TwoWay), after MyProperty triggered, it will fire TextChanged again.

 Public Overridable Property MyProperty As String
        Get
            Return _MyProperty
        End Get
        Set(ByVal value As String)
            _MyProperty = value

        End Set
    End Property

因此,我不建议您使用TextChanged事件,您可以尝试为TextBox设置LostFocus事件.

So, I don't suggest you use TextChanged event, you can try to LostFocus event for your TextBox.

<DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBox
                                Name="txtProp"
                                Text="{Binding Path=MyProperty, Mode=TwoWay,UpdateSourceTrigger=Explicit}"
                                    LostFocus="txtProp_LostFocus"
                                    FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"  />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>

      Private Sub txtProp_LostFocus(sender As Object, e As RoutedEventArgs)
        Dim ctl As FrameworkElement = TryCast(sender, FrameworkElement)
        If ctl Is Nothing Then Return

        Dim index As String = (_vm.LogsDG.Count + 1).ToString.PadLeft(3, "0"c)
        Select Case ctl.Name
            Case "txtProp"
                'TextBox
                Dim txt As TextBox = DirectCast(ctl, TextBox)
                _vm.LogsDG.Add(String.Format("{0}) TextBox {1} - TextChanged: {2}", index, txt.GetHashCode(), txt.Text))
                txt.GetBindingExpression(TextBox.TextProperty).UpdateSource()
        End Select
    End Sub



最好的问候,



Best Regards,

吕汉楠


这篇关于WPF CellEditingTemplate和重复事件,如何避免呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 01:10