使用一個轉換器布爾變量轉換爲int :
在Xaml上,asssuming選項映射到您的MyChoice屬性上的1,2,3 :
<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}"/>
在轉換器,並指出,我沒有添加任何施法保護:
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;
}
}
而且你最好在你的ViewModel實現INotifyPropertyChanged的。