zoukankan      html  css  js  c++  java
  • 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是无法直接绑定变量的

  • 相关阅读:
    Shell编程(/bin/sh和/bin/bash) 迎客
    Ubuntu命令大全 迎客
    一位软件工程师的6年总结(转) 迎客
    Ubuntu中常见特殊符号 迎客
    Nginx 简介 迎客
    IO流File对象功能删除指定目录中的空白目录
    五步让你成为专家级程序员
    关于eclipse 中文注释时中文字体太小的问题解决
    实现一个小小的动态时钟
    深入解析HashMap、HashTable
  • 原文地址:https://www.cnblogs.com/xcsn/p/4476890.html
Copyright © 2011-2022 走看看