简体   繁体   English

如果任何控件已更改,则引发事件WPF

[英]Raise event if any control has been changed WPF

I want make validation in my WPF application. 我想在我的WPF应用程序中进行验证。 And the approach I want to use is I want to take custom action if any value of my controls has been changed. 我要使用的方法是,如果控件的任何值已更改,我想采取自定义操作。

How I can do that suppose I have control like this 假设我拥有这样的控制权,我该怎么做

<TextBox x:Name="TextBox1" Text="{Binding Path=Box1,UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name="TextBox2" Text="{Binding Path=Box2,UpdateSourceTrigger=PropertyChanged}" />

And If user change the value one of that control I want to create custom action for example (For my thinking ) 而且,如果用户更改该控件之一的值,那么我想创建自定义操作(例如,根据我的想法)

private void Window_Controls_Property_Changed(object sender,RoutedEventArgs e){
  if(((Control)sender)=="TextBox1")
    MessageBox.Show("Show message here of validation some control","Attention",MessageBoxButton.OK);
}

I'm still confusing combine the INotifyPropertyChanged or ValidationRule with displaying error what I want with MessageBox. 我仍然混淆INotifyPropertyChangedValidationRule与显示错误与MessageBox我想要的。 I'm still newbie using this kind of features. 我还是新手,正在使用这种功能。

I have read many articles about INotifyPropertyChanged or ValidationRule but I'm still don't know how to get the custom action like I said before. 我已经阅读了许多有关INotifyPropertyChangedValidationRule文章,但我仍然不知道如何像我之前所说的那样获得自定义操作。 And the custom validation may use another textbpx value to validate with the other textbox. 并且自定义验证可以使用另一个textbpx值与另一个文本框进行验证。

Any helps?.. 有帮助吗?

You can register the handler on the Container that hosts your TextBox's. 您可以在承载TextBox的Container上注册处理程序。 Like this: 像这样:

<StackPanel TextBox.LostFocus="TextBox_OnLostFocus">
    <TextBox x:Name="TextBox1" Text="{Binding Path=Box1,UpdateSourceTrigger=PropertyChanged}" />
    <TextBox x:Name="TextBox2" Text="{Binding Path=Box2,UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>

This way the same handler will be used for all TextBox's and you can query e.Source to find out which TextBox triggered the event. 这样,所有TextBox都将使用相同的处理程序,并且您可以查询e.Source找出哪个TextBox触发了事件。

for Validation in mvvm you can take IDataErrorInfo. 要在mvvm中进行验证,可以使用IDataErrorInfo。 there are examples all over the net. 网上有很多例子。

and if you really whant to do some special action when a property in your viewmodel change, you can simply subscribe to your own viewmodel PropertyChanged event and handle your stuff 如果您真的想在视图模型中的属性发生更改时采取一些特殊的操作,则只需订阅自己的视图模型PropertyChanged事件并处理您的东西

You may want to read this block post about BindingGroups and validation (and this related blog post ). 您可能需要阅读这一块后约BindingGroups和验证(这相关的博客帖子 )。 They show you how you can validate multiple controls at once using custom validation rules. 它们向您展示了如何使用自定义验证规则一次验证多个控件。

<TextBox HorizontalAlignment="Left" LostFocus="OnLostFocus"/>

Here's an easy way. 这是一个简单的方法。 Validate using the LostFocus Event in WPF 在WPF中使用LostFocus事件进行验证

private void TextBox_LostFocus_1(object sender, RoutedEventArgs e) {
        var thisTextBox = (TextBox)sender;
        if (thisTextBox.Text == "") {
            MessageBox.Show("Validate!");
        }
    }

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

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