大佬教程收集整理的这篇文章主要介绍了WPF绑定复选框IsChecked到其他复选框(IsChecked ||!IsEnabled),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
>选中“全部”选中所有已启用的复选框.
>取消选中“全部”取消选中所有复选框.
>手动检查每个已启用的复选框将选中“全部”.
>选中所有已启用的复选框和“全部”并且用户取消选中任何复选框后,“全部”会自动取消选中.
我是WPF Databinding的新手.我想也许我只是点击“全部”来设置其他复选框.但我不知道如何绑定到多个源的两个属性.当我点击“全部”并手动取消选中另一个复选框时,“全部”需要取消选中.
更新
最后一部分(忽略禁用的checkBoX)在这里有点问题,我对这个解决方案并不是很疯狂,但我看不出更好的方法.我们存储Convert的值,并在ConvertBACk中重用它们用于禁用的checkBox.这样做,我们还应该为checkAllConverter添加x:Shared =“false”属性,因为每个使用它的MultiBinding都需要一个新实例(在这种情况下不太可能但仍然……)
<Window.resources> <local:checkAllConverter x:Key="checkAllConverter" x:Shared="false"/> </Window.resources> <StackPanel> <checkBox Content="check All" Name="checkAllcheckBox"> <checkBox.Ischecked> <MultiBinding Converter="{Staticresource checkAllConverter}"> <Binding ElementName="checkBox1" Path="Ischecked" /> <Binding ElementName="checkBox1" Path="IsEnabled" Mode="OneWay"/> <Binding ElementName="checkBox2" Path="Ischecked" /> <Binding ElementName="checkBox2" Path="IsEnabled" Mode="OneWay"/> <Binding ElementName="checkBox3" Path="Ischecked" /> <Binding ElementName="checkBox3" Path="IsEnabled" Mode="OneWay"/> </MultiBinding> </checkBox.Ischecked> </checkBox> <checkBox Content="checkBox 1" Name="checkBox1"/> <checkBox Content="checkBox 2" Name="checkBox2"/> <checkBox Content="checkBox 3" Name="checkBox3"/> </StackPanel>
checkAll转换器
public class checkAllConverter : IMultiValueConverter { private object[] convertValues = null; public object Convert(object[] values,Type targetType,object parameter,System.Globalization.CultureInfo culturE) { convertValues = new object[values.Length]; for(int i = 0; i < values.Length; i++) { convertValues[i] = values[i]; } for (int i = 0; i < values.Length; i += 2) { bool ischecked = (bool)values[i]; bool isEnabled = (bool)values[i + 1]; if (isEnabled == falsE) { conTinue; } if (ischecked == falsE) { return false; } } return true; } public object[] ConvertBACk(object value,Type[] targetTypes,System.Globalization.CultureInfo culturE) { object[] values = new object[targetTypes.Length]; for (int i = 0; i < values.Length; i += 2) { if (convertValues != null && (bool)convertValues[i + 1] == falsE) { values[i] = convertValues[i]; } else { values[i] = value; } // IsEnabled is OneWay and won't care about this value values[i + 1] = null; } return values; } }
以上是大佬教程为你收集整理的WPF绑定复选框IsChecked到其他复选框(IsChecked ||!IsEnabled)全部内容,希望文章能够帮你解决WPF绑定复选框IsChecked到其他复选框(IsChecked ||!IsEnabled)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。