2012-02-09 115 views
13
屬性

讓我們想象一下,我有:綁定單選按鈕組在WPF

<RadioButton GroupName="Group1" IsChecked="{Binding Path=RadioButton1IsChecked}" /> 
<RadioButton GroupName="Group1" IsChecked="{Binding Path=RadioButton2IsChecked}" /> 

然後在我的數據源I類有:

public bool RadioButton1IsChecked { get; set; } 
public bool RadioButton2IsChecked { get; set; } 
public enum RadioButtons { RadioButton1, RadioButton2, None } 
public RadioButtons SelectedRadioButton 
{ 
    get 
    { 
     if (this.RadioButtonIsChecked) 
      return RadioButtons.RadioButton1; 
     else if (this.RadioButtonIsChecked) 
      return RadioButtons.RadioButton2; 
     else 
      return RadioButtons.None; 
    } 
} 

我能以某種方式直接與我的單選按鈕SelectedRadioButton屬性?我真的需要RadioButton1IsCheckedRadioButton2IsChecked屬性只計算選定單選按鈕。

+0

This [blog post](http://blogs.msdn.com/b/mthalman/archive/2008/09/04/wpf-data-binding-with-radiobutton.aspx)可能有幫助 – 2012-02-09 16:22:42

+0

請參閱[我的回答在一個相關的問題(http://stackoverflow.com/questions/9145606/how-can-i-reduce-this-wpf-boilerplate-code/9145914#9145914),它應該幫助。 「SelectedItem」綁定到感興趣的屬性。 – 2012-02-09 18:39:42

+3

我喜歡:http://stackoverflow.com/questions/397556/how-to-bind-radiobuttons-to-an-enum – quetzalcoatl 2012-08-08 13:28:58

回答

15
<RadioButton GroupName="Group1" IsChecked="{Binding Path=SelectedRadioButton, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=RadioButton1}" /> 
<RadioButton GroupName="Group1" IsChecked="{Binding Path=SelectedRadioButton, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=RadioButton2}" /> 
public enum RadioButtons { RadioButton1, RadioButton2, None } 
public RadioButtons SelectedRadioButton {get;set;} 

public class EnumBooleanConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      var ParameterString = parameter as string; 
      if (ParameterString == null) 
       return DependencyProperty.UnsetValue; 

      if (Enum.IsDefined(value.GetType(), value) == false) 
       return DependencyProperty.UnsetValue; 

      object paramvalue = Enum.Parse(value.GetType(), ParameterString); 
      return paramvalue.Equals(value); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      var ParameterString = parameter as string; 
      var valueAsBool = (bool) value; 

      if (ParameterString == null || !valueAsBool) 
      { 
       try 
       { 
        return Enum.Parse(targetType, "0"); 
       } 
       catch (Exception) 
       { 
        return DependencyProperty.UnsetValue; 
       } 
      } 
      return Enum.Parse(targetType, ParameterString); 
     } 
    } 
+0

這是不是在'SelectedRadioButton'值設爲'RadioButtons.None '(通過執行'返回Enum.Parse(TARGETTYPE, 「0」); ')每當任何單選按鈕的'IsChecked'屬性被設置爲'FALSE'? – 2012-09-22 08:43:32

21

聲明類似於枚舉:

enum RadioOptions {Option1, Option2} 

XAML:

<RadioButton IsChecked="{Binding SelectedOption, Converter={StaticResource EnumBooleanConverter}, ConverterParameter={x:Static local:RadioOptions.Option1}}"/> 
<RadioButton IsChecked="{Binding SelectedOption, Converter={StaticResource EnumBooleanConverter}, ConverterParameter={x:Static local:RadioOptions.Option2}}"/> 

Converter類:

public class EnumBooleanConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return value.Equals(parameter); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return ((bool)value) ? parameter : Binding.DoNothing; 
    } 
} 
+0

與接受的答案有什麼不同? – Jerther 2016-03-02 21:34:18

+2

@Jerther:接受的答案需要一個字符串參數,並將其轉換爲一個枚舉。這個直接接受枚舉值,這是一個好得多的想法,因爲如果參數不正確,它將無法編譯。 – Artfunkel 2017-02-13 20:57:42

-1

XAML:

<RadioButton IsChecked="{Binding Path=SelectedOption, UpdateSourceTrigger=PropertyChanged}">Option1</RadioButton> 
<RadioButton IsChecked="{Binding Path=SelectedOption, UpdateSourceTrigger=PropertyChanged, Converter={v:NotBoolenConverter}}">Option2</RadioButton> 

轉換器:

public class NotBoolenConverter : IValueConverter 
    { 
     public NotBoolenConverter() 
     { 
     } 

     public override object Convert(
      object value, 
      Type targetType, 
      object parameter, 
      CultureInfo culture) 
     { 
      bool output = (bool)value; 
      return !output; 
     } 

     public override object ConvertBack(
      object value, 
      Type targetType, 
      object parameter, 
      CultureInfo culture) 
     { 
      bool output = (bool)value; 
      return !output; 
     } 
    } 

作品與2個的單選按鈕,通過結合一個到另一個的相反。