使用场景:多个RadioButton如何通过一个值进行绑定?
注意:不需要修改的可以使用,如果涉及到选中修改的话 建议还是使用转换器。
解决方案:CalcBinding支持在Binding中写简化的判断逻辑。
使用方法如下:
首先在NuGet中安装CalcBinding
csharp
<RadioButton
Height="60"
Content="A"
FontSize="14"
FontWeight="Bold"
Foreground="Black"
GroupName="StartRun"
IsChecked="{calc:Binding 'OperationIndex == 0'}"
Style="{StaticResource MinCustomRadioButtonStyle}" />
<RadioButton
Height="60"
Content="B"
FontSize="14"
FontWeight="Bold"
Foreground="Black"
GroupName="StartRun"
IsChecked="{calc:Binding 'OperationIndex == 1'}"
Style="{StaticResource MinCustomRadioButtonStyle}" />
<RadioButton
Grid.Column="1"
Height="60"
Content="C"
FontSize="14"
FontWeight="Bold"
Foreground="Black"
GroupName="StartRun"
IsChecked="{calc:Binding 'OperationIndex == 2'}"
Style="{StaticResource MinCustomRadioButtonStyle}" />
扩展:
csharp
<!-- 简单的相等比较 -->
<RadioButton Content="选项一"
IsChecked="{calc:Binding 'SelectedIndex == 1'}" />
<!-- 枚举比较 -->
<RadioButton Content="男性"
IsChecked="{calc:Binding 'Gender == Gender.Male'}" />
<!-- 字符串比较 -->
<RadioButton Content="北京"
IsChecked="{calc:Binding 'City == \"Beijing\"'}" />
<!-- 复杂逻辑表达式 -->
<RadioButton Content="有效用户"
IsChecked="{calc:Binding 'IsActive && Age >= 18'}" />
完结。