在尝试做一些更复杂的事情时,我遇到了一种我不太了解的行为。

假设下面的以下代码处理文本变换事件。

 private void textChanged(object sender, TextChangedEventArgs e)
    {
        TextBox current = sender as TextBox;
        current.Text = current.Text + "+";
    }

现在,在文本框中键入字符(例如A)将导致事件被绊倒两次(添加两个'+'s),最终文本显示仅为a+。

我的两个问题是,为什么活动仅两次?为什么只有第一个通过事件运行实际设置了文本框的文本?

提前致谢!

有帮助吗?

解决方案

好 - 在更改文本属性 /刚刚更改时设置文本属性似乎被 文本框 明确的课程:

只需使用反射器来看内部 TextBox.OntextPropertychanged (缩短):

TextBox box = (TextBox) d;
if (!box._isInsideTextContentChange)
{
    string newValue = (string) e.NewValue;
    //...
    box._isInsideTextContentChange = true;
    try
    {
        using (box.TextSelectionInternal.DeclareChangeBlock())
        {
           //...
        } //Probably raises TextChanged here
    }
    finally
    {
        box._isInsideTextContentChange = false;
    }
    //...
}

_isinsideTextContentChange 设置为true 文字变化 活动被提升。更改时 文本 再次属性, 文字变化 因此,事件不再被提出。

因此:功能;-)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top