WPF RadioButton変換

11828 ワード

モデル#モデル#
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は変数を直接バインドできません.