2013-08-01 11 views
7

Sliverlight, grup isminin grup seçimini yapmak için sadece bir seçenekle birlikte gruplandırmak için GroupName ile radyo düğmesi sağlar. Silverlight'ta grup radyo düğmesi için veri bağlama nasıl ayarlanır?

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

Sanal Makinede, bu seçenek için bir mülk, UI ve VM arasında bu durum için bağlayıcı nasıl kurulum verileri daha sonra MyChoice

public int MyChoice {get; set;} 

söylemek okunur: Sanki?

cevap

13

bir int bool değerlerden oluşan dönüştürmek için bir dönüştürücü kullanılır :

Xaml, MyChoice özelliğinizde 1,2,3 eş konum seçenekleri haritayı eşleme

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

Ayrıca size ViewModel INotifyPropertyChanged uygulamak için iyi olur: Ben herhangi bir döküm koruması eklemek etmediğini belirterek dönüştürücü olarak

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

.

0

Merhaba Eğer ViewModel türü bool üç özelliklerini oluşturmak ve 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> 

ait isChecked özelliğine bağlamak zorunda kalacak

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