XAML端
<RadioButton GroupName="condition" IsChecked="{Binding CurrentOption,Converter={StaticResource OptionConverter},ConverterParameter=全部}">全部</RadioButton> <RadioButton GroupName="condition"Margin="10,0,0,0" IsChecked="{Binding CurrentOption,Converter={StaticResource OptionConverter},ConverterParameter=内墙}">内墙</RadioButton> <RadioButton GroupName="condition" Margin="10,0,1,0" IsChecked="{Binding CurrentOption,Converter={StaticResource OptionConverter},ConverterParameter=外墙}">外墙</RadioButton>
转换器
public class OptionConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null || parameter == null) return false; string checkvalue = value.ToString(); string targetvalue = parameter.ToString(); bool r = checkvalue.Equals(targetvalue, StringComparison.InvariantCultureIgnoreCase); return r; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null || parameter == null) return "全部"; bool usevalue = (bool)value; if (usevalue) return parameter.ToString(); return null; } }
ViewModel
private string currentOption; public string CurrentOption { get { return currentOption; } set { if (currentOption != value) { currentOption = value; OnPropertyChanged("CurrentOption"); } } }