是否有 WPF 复选框控件切换事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12412860/
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-09-13 05:20:31  来源:igfitidea点击:

Is there a WPF check box control toggle event?

c#wpfcheckboxtoggle

提问by IneedHelp

Considering WPF controls, how do I know if a check box's value has changed (toggled)?

考虑到 WPF 控件,我如何知道复选框的值是否已更改(切换)?

I know there are the common Checked, Unchecked, Clickedevents, but how about an event for when the value changes, regardless of how it was changed?

我知道有常见的Checked, Unchecked,Clicked事件,但是值发生变化时的事件如何,不管它是如何变化的?

I looked through the events and I didn't find anything, but maybe I'm missing the obvious (as it has happened many times in the past).

我查看了事件并没有找到任何东西,但也许我错过了显而易见的事情(因为它在过去发生过很多次)。

回答by Erre Efe

You can just bind IsCheckedDependency Property to a boolean. On that binded property setter you can manipulate what you want (independently if it's setting it to true or false). That works just as expected.

您可以将IsChecked依赖属性绑定到布尔值。在该绑定属性设置器上,您可以操作您想要的内容(独立地将其设置为 true 或 false)。这正如预期的那样工作。

On your view:

在您看来:

  <Grid>
    <CheckBox ... IsChecked="{Binding ShowPending}"/>
  </Grid>

On your DataContext ViewModel or CodeBehind.

在您的 DataContext ViewModel 或 CodeBehind 上。

  private bool showPending = false;

  public bool ShowPending
  {
      get { return this.showPending; }
      set 
      { 
         //Here you mimic your Toggled event calling what you want!
         this.showPending = value; 
      }
  }

回答by Malchia7

I know this already has an accepted answer, but binding is a bit overkill on this.

我知道这已经有一个可以接受的答案,但是绑定在这方面有点矫枉过正。

Just write one event handler and wire it to both the Checked and Unchecked events then check the IsChecked property inside your event handler.

只需编写一个事件处理程序并将其连接到 Checked 和 Unchecked 事件,然后检查事件处理程序中的 IsChecked 属性。

回答by tylerjgarland

Going off Randolf's answer, just create a class representing your window. In the new class, create a property called BlahIsChecked. Implement the INotifyPropertChangedEvent in the class and in the setter of the the new property, fire the event with the property name.

离开 Randolf 的回答,只需创建一个代表您的窗口的类。在新类中,创建一个名为 BlahIsChecked 的属性。在新属性的类和 setter 中实现 INotifyPropertChangedEvent,使用属性名称触发事件。

class Blah : INotifyPropertyChanged
{
    // Used for triggering the event
    public event PropertyChangedEventHandler PropertyChanged;

    // Called when the property changes
    protected void OnPropertyChanged(String propertyName)
    {
        // Retrieve handler
        PropertyChangedEventHandler handler = this.PropertyChanged;
        // Check to make sure handler is not null
        if(handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private bool _blahIsChecked;
    public bool BlahIsChecked
    {
        get {
            return _blahIsChecked;
        }
        set {
            _blahIsChecked = value;
            OnPropertyChanged("BlahIsChecked);
        }
    }
}

Now, go to your wpf class and say this.DataContext = new MainModel(); You can do this in WPF or c#.

现在,转到您的 wpf 类并说 this.DataContext = new MainModel(); 您可以在 WPF 或 c# 中执行此操作。

Now in your checkbox xaml do the following

现在在您的复选框 xaml 中执行以下操作

<checkbox Checked="{Binding BlahIsChecked, Mode=TwoWay}"/>

I did this from memory but should get you started. Good luck.

我是凭记忆做到的,但应该能让你开始。祝你好运。

回答by sellmeadog

Your best option is probably the IsCheckedproperty. But if you require an event, you can look at creating a DependencyPropertyDescriptorand registering a handler with the AddValueChangedmethod.

您最好的选择可能是IsChecked物业。但是如果您需要一个事件,您可以查看创建一个DependencyPropertyDescriptor并使用该AddValueChanged方法注册一个处理程序。

I think this is about as close as you'll get to an evented notification that the check box's value has changed. Creating the descriptor and adding the handler looks something like this:

我认为这与您收到复选框值已更改的事件通知差不多。创建描述符并添加处理程序看起来像这样:

var dpd = DependencyPropertyDescriptor.FromProperty(CheckBox.IsChecked, typeof(CheckBox));
dpd.AddValueChanged(...);