【问题标题】:How to set up data binding for group radio button in Silverlight?如何在 Silverlight 中为组单选按钮设置数据绑定?
【发布时间】:2013-08-02 00:26:23
【问题描述】:

Sliverlight 提供了带有 GroupName 的单选按钮,可以将单选按钮组合在一起,只有一个选项可以从多个选项中选择。就像:

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

然后在 VM 中,我只有一个属性用于此选项,例如 MyChoice

public int MyChoice {get; set;}

那么这种情况下如何在 UI 和 VM 之间设置数据绑定呢?

【问题讨论】:

    标签: c# silverlight xaml data-binding silverlight-5.0


    【解决方案1】:

    使用转换器将 bool 转换为 int:

    在 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。

    【讨论】:

      【解决方案2】:

      您好,您必须创建三个 bool 类型的属性并绑定到 RadioButton 的 IsChecked 属性

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

      视图模型

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-24
        • 2017-07-25
        • 2017-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多