2013-08-01 11 views
7

Sliverlight fornisce un pulsante di opzione con GroupName per raggruppare il radiobutton insieme a una sola opzione dalla scelta multipla. E 'come:Come impostare il binding dei dati per il pulsante di scelta del gruppo in Silverlight?

<RadioButton GroupName="Option" Content="Option 1"></RadioButton> 
<RadioButton GroupName="Option" Content="Option 2"></RadioButton> 
<RadioButton GroupName="Option" Content="Option 3"></RadioButton> 

poi in VM, ho solo una proprietà per questa opzione, dire che è MyChoice

public int MyChoice {get; set;} 

quindi come dati di configurazione vincolante per questo caso tra UI e VM?

risposta

13

Utilizzato un convertitore per convertire Caccio ad un int :

Su Xaml, le opzioni di assunzioni sono mappate a 1,2,3 sulla proprietà MyChoice :

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

Nel convertitore, notando che non ho aggiunto alcuna protezione Cast:

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

anche Faresti meglio a implementare INotifyPropertyChanged in voi ViewModel.

0

Hi si dovrà creare tre proprietà di tipo bool e si legano alla proprietà IsChecked di 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; 
    }