How to set up data binding for group radio button in Silverlight?

断了今生、忘了曾经 提交于 2019-12-05 03:42:14

Used a converter to convert bools to an int:

On Xaml, asssuming options map to 1,2,3 on your MyChoice property:

    <RadioButton GroupName="Option" Content="Option 1"
                 IsChecked="{Binding Path=MyChoice, Converter={StaticResource RadioButtonToIntConverter}, 
        ConverterParameter=1}"/>
    <RadioButton GroupName="Option" Content="Option 2"
                 IsChecked="{Binding Path=MyChoice, Converter={StaticResource RadioButtonToIntConverter}, 
        ConverterParameter=2}"/>
    <RadioButton GroupName="Option" Content="Option 3"
                 IsChecked="{Binding Path=MyChoice, Converter={StaticResource RadioButtonToIntConverter}, 
        ConverterParameter=3}"/>

In the converter, noting that I did not add any cast protection:

public class RadioButtonToIntConverter:IValueConverter 
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var para = System.Convert.ToInt32(parameter);
        var myChoice = System.Convert.ToInt32(value);
        return para == myChoice;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var para = System.Convert.ToInt32(parameter);
        var isChecked = System.Convert.ToBoolean(value);
        return isChecked ? para : Binding.DoNothing;
    }
}

Also you'd better to implement INotifyPropertyChanged in you ViewModel.

Hi you will have to create three properties of type bool and bind to IsChecked property of RadioButton

<StackPanel>
        <RadioButton GroupName="Option" Content="Option 1" IsChecked="{Binding MyChoice1}"></RadioButton>
        <RadioButton GroupName="Option" Content="Option 2" IsChecked="{Binding MyChoice2}"></RadioButton>
        <RadioButton GroupName="Option" Content="Option 3" IsChecked="{Binding MyChoice3}"></RadioButton>
    </StackPanel>

ViewModel

        public class ViewModel:INotifyPropertyChanged
    {
        bool myChoice1;

        public bool MyChoice1
        {
            get { return myChoice1; }
            set { 
                myChoice1 = value;
                Notify("MyChoice1");
            }
        }
        bool myChoice2;

        public bool MyChoice2
        {
            get { return myChoice2; }
            set
            {
                myChoice2 = value;
                Notify("MyChoice2");
            }
        }
        bool myChoice3;

        public bool MyChoice3
        {
            get { return myChoice3; }
            set
            {
                myChoice3 = value;
                Notify("MyChoice3");
            }
        }

        public void Notify(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));

        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!