2012-02-09 11 views
13

Immaginiamo che ho:radiobuttons vincolanti gruppo a una proprietà in WPF

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

E poi nella mia classe di origine di dati che ho:

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; 
    } 
} 

Posso in qualche modo legare i miei pulsanti di opzione direttamente a SelectedRadioButton proprietà? Ho davvero bisogno delle proprietà RadioButton1IsChecked e RadioButton2IsChecked solo per calcolare il radiobutton selezionato.

+0

questo [post di blog] (http://blogs.msdn.com/b/mthalman/archive/2008/09/04/wpf-data-binding-with-radiobutton.aspx) può aiutare –

+0

Vedere [la mia risposta su una domanda correlata] (http://stackoverflow.com/questions/9145606/how-can-i-reduce-this-wpf-boilerplate-code/9145914#9145914), dovrebbe essere d'aiuto. Il 'SelectedItem' si lega alla proprietà di interesse. –

+3

Preferisco: http://stackoverflow.com/questions/397556/how-to-bind-radiobuttons-to-an-enum – quetzalcoatl

risposta

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

Non impostare il valore 'SelectedRadioButton' su' RadioButtons.None '(eseguendo' return Enum.Parse (targetType, "0"); ') ogni volta che una delle opzioni dei pulsanti radio '' IsChecked' è impostata su 'false'? –

21

Dichiarare un'enumerazione simile a:

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}}"/> 

classe 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

Qual è la differenza funzionale con la risposta accettata? – Jerther

+2

@Jerther: la risposta accettata accetta un parametro stringa e lo converte in enum. Questo accetta direttamente un valore enum, che è un'idea molto migliore in quanto non riuscirà a compilare se un parametro non è corretto. – Artfunkel

-1

XAML:

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

Converter:

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; 
     } 
    } 

Impianti con 2 pulsanti, legandosi uno al contrario dell'altro.

Problemi correlati