Simple WPF RadioButton Binding?

后端 未结 8 999
难免孤独
难免孤独 2020-12-12 16:00

What is the simplest way to bind a group of 3 radiobuttons to a property of type int for values 1, 2, or 3?

8条回答
  •  爱一瞬间的悲伤
    2020-12-12 16:47

    I came up with a simple solution.

    I have a model.cs class with:

    private int _isSuccess;
    public int IsSuccess { get { return _isSuccess; } set { _isSuccess = value; } }
    

    I have Window1.xaml.cs file with DataContext set to model.cs. The xaml contains the radiobuttons:

    
    
    
    

    Here is my converter:

    public class RadioBoolToIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int integer = (int)value;
            if (integer==int.Parse(parameter.ToString()))
                return true;
            else
                return false;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return parameter;
        }
    }
    

    And of course, in Window1's resources:

    
        
    
    

提交回复
热议问题