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'}" />

完结。

相关推荐
我要打打代码3 分钟前
C# 扩展方法
开发语言·c#
椒颜皮皮虾྅10 分钟前
OpenVINO™ C# API 3.3 全新发布!正式接入 OpenVINO GenAI,C# 本地大模型开发全面启航!
人工智能·开源·c#·openvino
云草桑16 分钟前
跨境信息系统术语研究 —— 产品、单据、身份名片的中文译法演变历程
面试·.net·odoo·erp·跨境
小满Autumn43 分钟前
雷赛DMC运动控制卡 — C#开发完全指南
c#·.net·上位机·运动控制卡·雷赛
专注VB编程开发20年10 小时前
AI 生成C# WinForm 窗体 = 目前就是垃圾
开发语言·人工智能·c#
z落落11 小时前
C# 泛型接口和泛型类+泛型约束
开发语言·c#
阿正的梦工坊11 小时前
【Rust】08-集合类型、字符串与迭代器入门
开发语言·rust·c#
FuckPatience11 小时前
C# 使用泛型协变将派生类类型替换为基类类型
开发语言·c#
guygg8811 小时前
C# 生成中间带 Logo 头像的二维码
开发语言·c#