Simple WPF RadioButton Binding?

后端 未结 8 989
难免孤独
难免孤独 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:58

    This example might be seem a bit lengthy, but its intention should be quite clear.

    It uses 3 Boolean properties in the ViewModel called, FlagForValue1, FlagForValue2 and FlagForValue3. Each of these 3 properties is backed by a single private field called _intValue.

    The 3 Radio buttons of the view (xaml) are each bound to its corresponding Flag property in the view model. This means the radio button displaying "Value 1" is bound to the FlagForValue1 bool property in the view model and the other two accordingly.

    When setting one of the properties in the view model (e.g. FlagForValue1), its important to also raise property changed events for the other two properties (e.g. FlagForValue2, and FlagForValue3) so the UI (WPF INotifyPropertyChanged infrastructure) can selected / deselect each radio button correctly.

        private int _intValue;
    
        public bool FlagForValue1
        {
            get
            {
                return (_intValue == 1) ? true : false;
            }
            set
            {
                _intValue = 1;
                RaisePropertyChanged("FlagForValue1");
                RaisePropertyChanged("FlagForValue2");
                RaisePropertyChanged("FlagForValue3");
            }
        }
    
        public bool FlagForValue2
        {
            get
            {
                return (_intValue == 2) ? true : false;
            }
            set
            {
                _intValue = 2;
                RaisePropertyChanged("FlagForValue1");
                RaisePropertyChanged("FlagForValue2");
                RaisePropertyChanged("FlagForValue3");
            }
        }
    
        public bool FlagForValue3
        {
            get
            {
                return (_intValue == 3) ? true : false;
            }
            set
            {
                _intValue = 3;
                RaisePropertyChanged("FlagForValue1");
                RaisePropertyChanged("FlagForValue2");
                RaisePropertyChanged("FlagForValue3");
            }
        }
    

    The xaml looks like this:

                    <RadioButton GroupName="Search" IsChecked="{Binding Path=FlagForValue1, Mode=TwoWay}"
                                 >Value 1</RadioButton>
    
                    <RadioButton GroupName="Search" IsChecked="{Binding Path=FlagForValue2, Mode=TwoWay}"
                                 >Value 2</RadioButton>
    
                    <RadioButton GroupName="Search" IsChecked="{Binding Path=FlagForValue3, Mode=TwoWay}"
                                 >Value 3</RadioButton>
    
    0 讨论(0)
  • 2020-12-12 17:01

    I am very suprised nobody came up with this kind of solution to bind it against bool array. It might not be the cleanest, but it can be used very easily:

    private bool[] _modeArray = new bool[] { true, false, false};
    public bool[] ModeArray
    {
        get { return _modeArray ; }
    }
    public int SelectedMode
    {
        get { return Array.IndexOf(_modeArray, true); }
    }
    

    in XAML:

    <RadioButton GroupName="Mode" IsChecked="{Binding Path=ModeArray[0], Mode=TwoWay}"/>
    <RadioButton GroupName="Mode" IsChecked="{Binding Path=ModeArray[1], Mode=TwoWay}"/>
    <RadioButton GroupName="Mode" IsChecked="{Binding Path=ModeArray[2], Mode=TwoWay}"/>
    

    NOTE: you dont need two-way binding if you dont want to one checked by default. TwoWay binding is the biggest cons of this solution.

    Pros:

    • No need for code behind
    • No need for extra class (IValue Converter)
    • No Need for extra enums
    • doesnt require bizzare binding
    • straightforward and easy to understand
    • doesnt violate MVVM (heh, at least I hope so)
    0 讨论(0)
提交回复
热议问题