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

完结。

相关推荐
周杰伦fans19 小时前
C# 中的**享元工厂**模式
开发语言·数据库·c#
鹿衔`20 小时前
通过Flink 1.19 客户端实现Flink集群连接 Kafka 基础测试报告
c#·linq
用户72278681234421 小时前
.NET 实现雪花算法:高效生成分布式唯一 ID
.net
玩泥巴的1 天前
.NET 8+ 飞书API实战:自动化群组管理与消息推送
c#·.net·二次开发·飞书
烛阴1 天前
从`new`关键字开始:精通C#类与对象
前端·c#
yangshuquan1 天前
使用 C# + IronOcr,轻松实现图片文字自动识别(OCR)和提取
c#·ocr·编程技巧·winforms
天天代码码天天1 天前
TSR18测速雷达C#对接
c#·雷达测速·tsr18测速雷达
道一231 天前
C#获取操作系统版本号方法
开发语言·c#
道一231 天前
C# 判断文件是否存在的方法
开发语言·c#
唐青枫1 天前
C#.NET 范围与索引(Range、Index)完全解析:语法、用法与最佳实践
c#·.net