WPF RadioButton的绑定

1. 枚举类

public enum EnumDataTypes
    {
        Simulation,
        Test
    }

 

2. 枚举型与布尔型的转换

public class EnumToBooleanConverter : IValueConverter
    {
        /// 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? false : value.Equals(parameter);
        }

        /// 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value != null && value.Equals(true) ? parameter : Binding.DoNothing;
        }
    }

 

3. 控件XAML的代码

<Window.Resources>
        <pv:EnumToBooleanConverter x:Key="EnumBoolConverter" />
    Window.Resources>

<RadioButton Grid.Column="0" Content="仿真" VerticalAlignment="Center"
                             Margin="10 5 0 5" 
                             IsChecked="{Binding Path=SelectedDatabase.DataType, 
                    Converter={StaticResource EnumBoolConverter}, ConverterParameter={x:Static pv:EnumDataTypes.Simulation}}"/>
                <RadioButton Grid.Column="1" Content="试验" VerticalAlignment="Center"
                             Margin="20 5 0 5"
                             IsChecked="{Binding Path=SelectedDatabase.DataType, 
                    Converter={StaticResource EnumBoolConverter}, ConverterParameter={x:Static pv:EnumDataTypes.Test}}"/>

 

转载于:https://www.cnblogs.com/xpvincent/p/10267320.html

你可能感兴趣的:(WPF RadioButton的绑定)