WPF Check box: Check changed handling

后端 未结 6 865
忘掉有多难
忘掉有多难 2020-12-01 13:33

In WPF data binding, I can bind the IsChecked property to some data, e.g. user setting, but I need to handle \"CheckChanged\" event, I know I can seperately handle <

6条回答
  •  一生所求
    2020-12-01 14:11

    That you can handle the checked and unchecked events seperately doesn't mean you have to. If you don't want to follow the MVVM pattern you can simply attach the same handler to both events and you have your change signal:

    
    

    and in Code-behind;

    private void CheckBoxChanged(object sender, RoutedEventArgs e)
    {
      MessageBox.Show("Eureka, it changed!");
    }
    

    Please note that WPF strongly encourages the MVVM pattern utilizing INotifyPropertyChanged and/or DependencyProperties for a reason. This is something that works, not something I would like to encourage as good programming habit.

提交回复
热议问题