2013-08-01 17 views
7

SliverlightはGroupNameを持つラジオボタンを提供し、複数の選択肢から1つのオプションだけでラジオボタンをグループ化します。それはのようだ:Silverlightでグループラジオボタンのデータバインドを設定する方法は?

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

は、VMで、私は、このオプションのプロパティを1つだけ持って、どのようにセットアップUIとVM間でこのような場合のためにデータバインディングをする、それが

public int MyChoice {get; set;} 

MyChoice

だと言いますか?

答えて

13

のにisCheckedプロパティにバインドする必要があります:

Xamlでは、オプションがMyChoiceプロパティの1,2,3にマップされます:コンバータで

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

、私はどのキャスト保護を追加していないことを指摘:

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

また、あなたはあなたのViewModelにINotifyPropertyChangedの実装をした方が良いと思います。

0

こんにちはあなたはbool型の3つのプロパティを作成して、ラジオボタン

intにboolsを変換するコンバータを使用し
<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; 
    } 
関連する問題