wpf 如何在不引发事件的情况下设置 checkbox.isChecked
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16595063/
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
How to set checkbox.isChecked without raising event
提问by Sturm
Is there a way of checking the CheckBoxwithout running the code associated to checking it? Just for visual appearance.
有没有办法在CheckBox不运行与检查相关的代码的情况下进行检查?只是为了视觉外观。
Edit:
编辑:
private void normalCheck_Checked(object sender, RoutedEventArgs e)
{
normal();
}
Imagine that I want to set the normalCheckBox.IsChecked=true;but without raising the event. Is that possible?
想象一下,我想设置normalCheckBox.IsChecked=true;但不引发事件。那可能吗?
回答by keyboardP
One way would be to detach the event handler, set the IsCheckedproperty, and then reattach it.
一种方法是分离事件处理程序,设置IsChecked属性,然后重新附加它。
myCheckbox.Checked -= myCheckbox_Checked;
myCheckbox.IsChecked = true;
myCheckbox.Checked += myCheckbox_Checked;
回答by Callum Watkins
You could use the Clickevent instead of Checkedand use the state of the checkbox like below:
您可以使用Click事件代替Checked并使用复选框的状态,如下所示:
private void normalCheck_Click(object sender, RoutedEventArgs e)
{
if (normalCheck.IsChecked ?? false) { normal(); }
}
Then, this event won't be raised by using normalCheck.IsChecked = true;. It will only be raised by a click.
然后,将不会通过使用引发此事件normalCheck.IsChecked = true;。它只会通过单击而升高。
NOTE: The null-coalescing operator (??) is necessary because IsCheckedreturns a bool?type which could be null.
注意:空合并运算符 ( ??) 是必需的,因为IsChecked返回的bool?类型可能为空。
回答by panhandel
If you're referring to changing the checked status without raising the "_Checked" event, you will likely have to override the event handler with a param to tell it to skip the event or not.
如果您指的是在不引发“_Checked”事件的情况下更改已检查状态,则可能必须使用参数覆盖事件处理程序以告诉它是否跳过该事件。
Related answer: Change Checkbox value without raising event
相关答案:在不引发事件的情况下更改复选框值

