• 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是无法直接绑定变量的

  • 相关阅读:
    在LINQTOSQL中实现“级联删除”的方法
    “BindingNavigator”如何在删除前弹出确认框?
    OOP设计思考——究竟是继承自普通类,还是继承自抽象类?
    ASP.NET控件为什么无法使用结构?
    如何消除Web自定义控件的“自生成”复合属性的冗余类名称?
    用C#动态输出js单引号问题
    关于ready和load方法作用于不同情况下的比较
    关于CodeSign error : Certificate identity 'iPhone Distribution *** : ...问题
    [iOS]Xcode4/iOS5调试UncaughtException崩溃First throw call stack不打印方法名的解决方案
    Lion版本Mac OS下查看iPhone Simulator目录
  • 原文地址:https://www.cnblogs.com/xcsn/p/4476890.html
Copyright © 2020-2023  润新知