WPF中的WinForms TextBox.Validating事件的等效项

Rya*_*ndy 5 .net c# vb.net wpf winforms

在WinForms中,用户更改了TextBox中的文本后,我可以处理Validated事件以执行某些操作。与TextChanged不同,Validated不会在每次字符更改时触发;仅在用户完成后才触发。

我可以使用WPF中的任何东西来获得相同的结果,只有在用户完成更改文本后才引发的事件?

Mar*_*ris 5

当用户从您的文本框移动到任何其他控件时,LostFocus将触发。


Nic*_*las 5

似乎没有本地解决方案。LostFocus 事件是个好主意。但是当用户单击 Enter 时,他希望文本框验证更改。所以这是我的建议:当按键为 Enter 时,使用 LostFocus 事件和 KeyDown 事件。

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    // code to lauch after validation
}

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        // call the LostFocus event to validate the TextBox
        ((TextBox)sender).RaiseEvent(new RoutedEventArgs(TextBox.LostFocusEvent));
    }
}
Run Code Online (Sandbox Code Playgroud)