wpf 中radioButton binding enum的时候的一个bug

View Code
  <RadioButton Margin="0,6,0,0" VerticalAlignment="Center"  Grid.Row="0" Grid.Column="1"   AutomationProperties.AutomationId="MaleRadioButton"
                         Content="{DynamicResource ResourceKey=PM_Male}" 
                         IsChecked="{Binding Gender,
                                             Mode=TwoWay,
                                             Converter={StaticResource enumToBooleanConverter1},
                                             ConverterParameter={x:Static inf:Gender.Male}}"/>
                        <RadioButton Margin="0,6,0,0" VerticalAlignment="Center"  Grid.Row="1" Grid.Column="1"  AutomationProperties.AutomationId="FemaleRadioButton"
                         Content="{DynamicResource ResourceKey=PM_Female}"
                         IsChecked="{Binding Gender,
                                             Mode=TwoWay,
                                             Converter={StaticResource enumToBooleanConverter1},
                                             ConverterParameter={x:Static inf:Gender.Female}}"/>
                        <RadioButton Margin="0,6,0,11" VerticalAlignment="Center"  Grid.Row="2" Grid.Column="1"  AutomationProperties.AutomationId="UnkonwnRadioButton"
                         Content="{DynamicResource ResourceKey=PM_Unkonwn}"
                         IsChecked="{Binding Gender, Mode=TwoWay,
                                             Converter={StaticResource enumToBooleanConverter1},
                                             ConverterParameter={x:Static inf:Gender.Unkonwn}}"/>

转换类

View Code
  public class EnumToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((Enum)value).HasFlag((Enum)parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.Equals(true) ? parameter : Binding.DoNothing;
        }
    }

 

enum

View Code
  public enum Gender
    {
        Default,
        Male,
        Female,
        Unkonwn
    }
 

如果不给gender写一个defult的enum。。。如果前台选中一个radiobutton(isChecked=true)。。然后再点击一下这个radiobutton。。会出现第一个radiobutton也被选上的bug。。解决办法。。给它加一个。。不需要的enum值。。作为第一个。。

posted @ 2013-03-18 17:13  法的空间  阅读(548)  评论(0编辑  收藏  举报