WPF CalcBinding简化判断逻辑

使用场景:多个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'}" />

完结。

相关推荐
Jeff-Tsai1 天前
Day 26:数值语义、多币种与计量单位
.net·架构设计·erp·definition-driven
风云2 天前
两条 Pipe、五个命门:Vane.NET 内核为什么长这样
开源·网络编程·.net·架构设计·vane.net
zdr尽职尽责2 天前
UnityCli 兼容2022版本
ui·unity·ai·c#·游戏引擎
宝桥南山2 天前
Visual Studio 2026新特性 - 可以直接连接DeepSeek(使用Custom Endpoint Provider配置项)
ide·microsoft·微软·aigc·.net·visual studio
Jeff-Tsai2 天前
Day 25:审计轨迹与变更的前后值
.net·架构设计·erp·definition-driven
IT规划师2 天前
Redis 核心机制 | 事务:执行序列的原子性、乐观锁与 8.x 条件写
redis·.net
Jeff-Tsai3 天前
Day 24:权限的三轴:动作、行与列
.net·架构设计·erp·definition-driven
BruceNeter3 天前
C# winform卡死问题分析1——线程池子线程创建句柄
c#·.net
samble3 天前
从零搭建半导体设备上位机(十二):参数设置页,表单校验、范围校验与配方联动
wpf·数据校验·validationrule