WPF RadioButton 绑定boolean值

xml 复制代码
<RadioButton
    Margin="5"
    Content="替换"
    IsChecked="{Binding CorrectionOption.ReCorrectionMode}" />
<RadioButton
    Margin="5"
    Content="平均"
    IsChecked="{Binding CorrectionOption.ReCorrectionMode, Converter={StaticResource ReCorrectionModeToBooleanConverter}}" />

切记:RadioButton 不能有GroupName属性

csharp 复制代码
public class ReCorrectionModeToBooleanConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool booleanValue)
        {
            return !booleanValue; // 取反
        }
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool booleanValue)
        {
            return !booleanValue; // 取反
        }
        return value;
    }
}
相关推荐
切糕师学AI4 分钟前
C# 如何回收整个 EF(DbContext)对象及其相关实体的内存?
c#·gc·垃圾回收·ef
momo小菜pa9 分钟前
C#--BindingList
开发语言·c#
我是唐青枫14 分钟前
C# 列表模式(List Patterns)深度解析:模式匹配再进化!
c#·.net
云草桑30 分钟前
Net 模拟退火,遗传算法,禁忌搜索,神经网络 ,并将 APS 排程算法集成到 ABP vNext 中
c#·.net·制造
乐科32 分钟前
WPF定时器
stm32·单片机·wpf
范小多1 小时前
mysql实战 C# 访问mysql(连载三)
数据库·mysql·oracle·c#
我是唐青枫4 小时前
C# 泛型数学:解锁真正的类型安全数值运算
c#·.net
故事不长丨9 小时前
C#定时器与延时操作的使用
开发语言·c#·.net·线程·定时器·winform
阿桂有点桂10 小时前
C#使用VS软件打包msi安装包
windows·vscode·c#
c#上位机10 小时前
halcon图像增强之分段灰度拉伸2
c#·上位机·halcon·机器视觉