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

完结。

相关推荐
Aevget2 小时前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(五)
wpf·界面控件·devexpress·ui开发·.net 10
【D'accumulation】2 小时前
.NET Framework 4.8 + Microsoft.Data.Sqlite 报 Library e_sqlite3 not found
microsoft·sqlite·.net
就是有点傻12 小时前
使用PaddleOCRSharp大模型精选文字识别
c#
LeonDL16813 小时前
【通用视觉框架】基于C#+Winform+OpencvSharp开发的视觉框架软件,全套源码,开箱即用
人工智能·c#·winform·opencvsharp·机器视觉软件框架·通用视觉框架·机器视觉框架
数据的世界0114 小时前
技术变革:为何C#与.NET是未来的开发方向
java·c#·.net
大龄Python青年14 小时前
C#快入教程:Linux安装.NET
linux·c#·.net
向上的车轮14 小时前
Actix Web适合什么类型的Web应用?可以部署 Java 或 .NET 的应用程序?
java·前端·rust·.net
我是唐青枫15 小时前
C#.NET Random 深入解析:随机数生成原理与最佳实践
c#·.net