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

完结。

相关推荐
A_nanda1 小时前
c# MOdbus rto读写串口,如何不相互影响
算法·c#·多线程
码云数智-园园3 小时前
使用 C# 将 PowerPoint 演示文稿高效转换为 PDF 格式
c#
听麟4 小时前
HarmonyOS 6.0+ 智慧出行导航APP开发实战:离线地图与多设备位置协同落地
华为·wpf·harmonyos
PfCoder4 小时前
WinForm真入门(23)---PictureBox 控件详细用法
开发语言·windows·c#·winform
gc_22997 小时前
C#学习调用OpenMcdf模块解析ole数据的基本用法(1)
c#·ole·openmcdf
MM_MS11 小时前
Halcon图像点运算、获取直方图、直方图均衡化
图像处理·人工智能·算法·目标检测·计算机视觉·c#·视觉检测
笨蛋不要掉眼泪12 小时前
Spring Boot + RedisTemplate 数据结构的基础操作
java·数据结构·spring boot·redis·wpf
老骥伏枥~13 小时前
C# 控制台:Console.ReadLine / WriteLine
开发语言·c#
PfCoder1 天前
C#中定时器之System.Timers.Timer
c#·.net·visual studio·winform
ahxdyz1 天前
.NET平台MCP
ai·.net·mcp