WPF 将复选框 IsChecked 绑定到其他复选框 (IsChecked || !IsEnabled)
我有一个选择“全部”复选框,我希望将其绑定到其他复选框的 (IsChecked || !IsEnabled)。
选中“全部”会检查所有已启用的复选框。
取消选中“全部”将取消选中所有复选框。
手动检查每个已启用的复选框将选中“全部”。
当选中所有启用的复选框和“全部”并且用户取消选中任何复选框时,“全部”会自动取消选中。
我是 WPF 数据绑定新手。我在想也许我只是处理“全部”的点击来设置其他复选框。但我不知道如何绑定到多个源的两个属性。当我单击“全部”并手动取消选中另一个复选框时,“全部”需要取消选中。
I have a select 'All' checkbox that I would like to be bound to the (IsChecked || !IsEnabled) of other checkboxes.
Checking 'All' checks all enabled checkboxes.
Unchecking 'All' unchecks all checkboxes.
Manually checking each enabled checkbox will check 'All'.
When all enabled checkboxes and 'All' are checked and user unchecks any checkbox, 'All' automatically unchecks.
I am new to WPF Databinding. I was thinking maybe I just handle the click of 'All' to set the other checkboxes. But I don't know how to bind to both properties of multiple sources. When I click 'All' and manually uncheck another checkbox, 'All' needs to uncheck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是使用转换器在 Xaml 中执行此操作的方法。这是假设所有 CheckBox 都直接添加为 Xaml 中的控件(它不是很动态,不适用于 DataTemplate 等)。首先,我们创建三个复选框(CheckBox1、CheckBox2、CheckBox3),在检查 CheckAllCheckBox 时将选中/取消选中它们。它也将起到相反的作用。
更新
最后一部分(忽略禁用的复选框)在这里有点问题,我对这个解决方案并不着迷,但我看不到更好的方法。我们存储来自 Convert 的值,并在 ConvertBack 中为禁用的复选框重新使用它们。为此,我们还应该为 CheckAllConverter 添加 x:Shared="False" 属性,因为每个使用它的 MultiBinding 都需要一个新实例(在这种情况下不太可能,但仍然......
)
Here's a way to do it in Xaml with Converters. This is assuming that all your CheckBox's are added directly as Controls in Xaml (it's not very dynamic, won't work for DataTemplate's etc). First we create three CheckBoxes (CheckBox1, CheckBox2, CheckBox3) that will be Checked/Unchecked when Checking the CheckAllCheckBox. It will also work in reverse.
Update
The last part (ignore disabled CheckBox) was a bit of a problem here and I'm not crazy about this solution but I can't see a better way. We store the values from Convert and re-use them in ConvertBack for the disabled CheckBox's. Doing this, we should also add the x:Shared="False" attribute for the CheckAllConverter since a new instance is required for every MultiBinding that would use it (unlikely in this case but still..)
The Converter for CheckAll
我将使用 MVVM 设计模式在 View 类后面创建一个 ViewModel: http:// /msdn.microsoft.com/en-us/magazine/dd419663.aspx
然后在您的 ViewModel(必须实现 INotifyPropertyChanged)上,您可以拥有多个
bool
属性,每个 CheckBox 一个属性还有一个适用于所有人:I'd create a ViewModel behind your View class using the MVVM design pattern: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
Then on your ViewModel (which will have to implement INotifyPropertyChanged) you can have multiple
bool
properties, one for each CheckBox and another for all of them: