C# WPF 复选框:检查更改的处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15480279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:57:59  来源:igfitidea点击:

WPF Check box: Check changed handling

c#wpfcheckbox

提问by David

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 Checked, Uncheckedevent, but is there any way to get notified when this value is changed?

在 WPF 数据绑定中,我可以将 IsChecked 属性绑定到一些数据,例如用户设置,但我需要处理“CheckChanged”事件,我知道我可以单独处理CheckedUnchecked事件,但是有什么方法可以在发生这种情况时得到通知值变了?

<CheckBox Content="Case Sensitive" IsChecked="{Binding bSearchCaseSensitive,
          Source={x:Static Properties:Settings.Default}}" />

Note: I don't care if it is checked or unchecked. I just want to be notified when it is changed.

注意:我不在乎它是选中还是未选中。我只想在更改时收到通知。

回答by dowhilefor

What about the Checkedevent? Combine that with AttachedCommandBehaviorsor something similar, and a DelegateCommandto get a function fired in your viewmodel everytime that event is called.

怎么样经过事件?将它与AttachedCommandBehaviors或类似的东西和DelegateCommand结合起来,以便在每次调用该事件时在您的视图模型中触发一个函数。

回答by Federico Berasategui

Im putting this in an answer because it's too long for a comment:

我把它放在一个答案中,因为评论太长了:

If you need the VM to be aware when the CheckBoxis changed, you should really bind the CheckBoxto the VM, and not a static value:

如果您需要 VM 知道 何时CheckBox更改,您应该真正将 绑定CheckBox到 VM,而不是静态值:

public class ViewModel
{
    private bool _caseSensitive;
    public bool CaseSensitive
    {
        get { return _caseSensitive; }
        set
        {
            _caseSensitive = value;
            NotifyPropertyChange(() => CaseSensitive);

            Settings.Default.bSearchCaseSensitive = value;
        }
    }
}

XAML:

XAML:

<CheckBox Content="Case Sensitive" IsChecked="{Binding CaseSensitive}"/>

回答by nvoigt

That you canhandle 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:

可以分别处理选中和未选中的事件并不意味着您必须这样做。如果您不想遵循 MVVM 模式,您可以简单地将相同的处理程序附加到两个事件,然后您就有了更改信号:

<CheckBox Checked="CheckBoxChanged" Unchecked="CheckBoxChanged"/>

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.

请注意,出于某种原因,WPF 强烈鼓励使用 INotifyPropertyChanged 和/或 DependencyProperties 的 MVVM 模式。这是有效的,而不是我想鼓励的良好编程习惯。

回答by user1568891

I know this is an old question, but how about just binding to Commandif using MVVM?

我知道这是一个老问题,但是Command如果使用 MVVM只绑定到怎么样?

ex:

前任:

<CheckBox Content="Case Sensitive" Command="{Binding bSearchCaseSensitive}"/>

For me it triggers on both Checkand Uncheck.

对我来说,它同时触发CheckUncheck

回答by Rob

As a checkbox click = a checkbox change the following will also work:

作为复选框单击 = 复选框更改,以下内容也将起作用:

<CheckBox Click="CheckBox_Click" />
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
    // ... do some stuff
}

It has the additional advantage of working when IsThreeState="True"whereas just handling Checked and Unchecked does not.

它具有工作的额外优势,IsThreeState="True"而仅处理 Checked 和 Unchecked 则不然。

回答by Syph3r-

A simple and proper way I've found to Handle Checked/Unchecked events using MVVM pattern is the Following, with Caliburn.Micro :

我发现使用 MVVM 模式处理已检查/未检查事件的一种简单而正确的方法如下,使用 Caliburn.Micro :

 <CheckBox IsChecked="{Binding IsCheckedBooleanProperty}" Content="{DynamicResource DisplayContent}" cal:Message.Attach="[Event Checked] = [Action CheckBoxClicked()]; [Event Unchecked] = [Action CheckBoxClicked()]" />

And implement a Method CheckBoxClicked() in the ViewModel, to do stuff you want.

并在 ViewModel 中实现方法 CheckBoxClicked() 来做你想做的事情。