简体   繁体   English

如何处理Devexpress WPF TextBox更改事件?

[英]how to handle Devexpress WPF TextBox Changed Event?

i am new user in devexpress WPF app. 我是devexpress WPF应用程序的新用户。 i really want to learn how to detect any changes on textbox event? 我真的想学习如何检测文本框事件的任何变化? For example; 例如; there are 2 textbox (devexpress) (txt1,txt2) . 有2个文本框(devexpress)(txt1,txt2)。 if i erase values on txt1, txt2 must erase own values. 如果我擦除txt1上的值,则txt2必须擦除自己的值。

like that: 像那样:

  private void txt1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Delete)
        {
            txt2.Text = String.Empty;
        }
    }

is it true? 是真的吗 Can you help me? 你能帮助我吗?

If the text of txt2 has to be exactly the same like in txt1, use binding: 如果txt2的文本必须与txt1中的文本完全相同,请使用binding:

<TextBox Name="txt2" Text="{Binding ElementName=txt1, Path=Text}"/>

If you just want to get the changes, try this (using the TextChanged-Event instead of KeyDown, because you also can paste strings into textboxes): 如果只想获取更改,请尝试以下操作(使用TextChanged-Event而不是KeyDown,因为您也可以将字符串粘贴到文本框中):

string oldtext = "";
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    string removedstring = "";
    string addedstring = "";
    TextBox source = (TextBox)e.Source;
    TextChange t = e.Changes.First();

    if (t.RemovedLength > 0)
    {
        removedstring = oldtext.Substring(t.Offset, t.RemovedLength);
    }

    if (t.AddedLength > 0)
    {
        addedstring = source.Text.Substring(t.Offset, t.AddedLength);
    }

    oldtext = source.Text;
}

If you want to set txt2.Text = txt1.Text by code 如果要通过代码设置txt2.Text = txt1.Text

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox source = (TextBox)e.Source;
        TextChange t = e.Changes.First();

        string first = txt2.Text.Substring(0, t.Offset);
        string added = source.Text.Substring(t.Offset, t.AddedLength);
        string last = (t.Offset+1>tbrt.Text.Length)?"":txt2.Text.Substring(t.Offset, txt2.Text.Length-1);
        last = last.Remove(0, t.RemovedLength);
        txt2.Text = first + added + last;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM