• WPF RadioButton 转换


    模型

    public class people
    {
       public string name{get;set;}       
       public bool? sex{get;set;}       
    }

    转换器

    namespace Helper
    {
        public class StringRadioConvert : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value == null || parameter == null)
                    return false;
                string checkvalue = value.ToString();
                string targetvalue = parameter.ToString();
                bool r = checkvalue.Equals(targetvalue, StringComparison.InvariantCultureIgnoreCase);
                return r;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value == null || parameter == null)
                    return null;
                bool usevalue = (bool)value;
                if (usevalue)
                    return parameter.ToString();
                return null;
            }
        }
        /// <summary>
        /// BOOL TO BOOL 
        /// </summary>
        public class BoolRadioConvert : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value == null || parameter == null)
                    return false;
    
                bool flag = (bool)value;
    
    
                if ((flag && (string)parameter == "") || (!flag && (string)parameter == ""))
                {
                    return true;
                }
    
                return false;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value == null || parameter == null)
                    return null;
                bool usevalue = (bool)value;
                if (!usevalue)
                    return null;
                Dictionary<string, bool> dict = new Dictionary<string, bool>();
                dict.Add("", true);
                dict.Add("", false);
    
                return dict[parameter.ToString()];
            }
        }
    
    }

    VIEW

    <UserControl ......
                 xmlns:helper="clr-namespace:Helper"......>
        <UserControl.Resources>
            <helper:StringRadioConvert x:Key="radioStringConverter" />
            <helper:BoolRadioConvert x:Key="radioBoolConverter" />
       </UserControl.Resources>
    <Label VerticalAlignment="Center" HorizontalAlignment="Right" FontWeight="ExtraBlack">用户:</Label> <RadioButton Content="小陈" GroupName="SeatGroup" IsChecked="{Binding people.name, Converter={StaticResource radioStringConverter}, ConverterParameter='小陈'}"></RadioButton> <RadioButton Content="小李" GroupName="SeatGroup" IsChecked="{Binding people.name, Converter={StaticResource radioStringConverter}, ConverterParameter='小李'}" ></RadioButton>
    <Label VerticalAlignment="Center" HorizontalAlignment="Right" FontWeight="ExtraBlack">输血史:</Label> <RadioButton Margin="4 0" GroupName="TransfusionGroup" IsChecked="{Binding people.sex , Converter={StaticResource radioBoolConverter}, ConverterParameter='男'}">男</RadioButton> <RadioButton Margin="4 0" GroupName="TransfusionGroup" IsChecked="{Binding people.sex , Converter={StaticResource radioBoolConverter}, ConverterParameter='女'}">女</RadioButton> </UserControl>

    解析:

    name为string类型,转化为bool

    sex需定义为bool?类型,否则会出现红框提示,此外,IsChecked是无法直接绑定变量的

  • 相关阅读:
    OpenStack 中的neutron-server启动过程
    NYOJ 284 坦克大战 【BFS】+【优先队列】
    HDSF主要节点解说(二)工作原理
    SQL SERVER中的流程控制语句
    Android 自己定义View (二) 进阶
    JNI学习积累之一 ---- 常用函数大全
    Android NDK开发之Jni的数据类型
    CMakeListx.txt 编辑语法学习
    用CMake代替makefile进行跨平台交叉编译
    Android 开发--CMakeList调用本地so文件
  • 原文地址:https://www.cnblogs.com/xcsn/p/4476890.html
Copyright © 2020-2023  润新知