2

我正在努力解决这个问题?在我的虚拟机中nullable bool,我有一个要绑定到 Yes/No( true/false )的属性RadRadioButton。有人可以指出我正确的方向吗?

虚拟机

public bool? IsSDS { get; set; }

看法

<telerik:Label Content="Self Directed Support" />
<StackPanel Orientation="Horizontal">
     <telerik:RadRadioButton x:Name="SelfDirectedSupportYes" Content="Yes" />
     <telerik:RadRadioButton x:Name="SelfDirectedSupportNo" Content="No" />
</StackPanel>

编辑

我认为这是有效的,但我错了。只有 Yes 具有约束力。以下是我的观点,CreateAuthView

<StackPanel Orientation="Horizontal" Margin="3" Grid.Column="1" Grid.Row="2">
   <telerik:RadRadioButton x:Name="Authorization_SelfDirectedSupportYes" GroupName="SDS" Content="Yes" />
    <telerik:RadRadioButton x:Name="Authorization_SelfDirectedSupportNo" GroupName="SDS" Content="No" />
</StackPanel>

这是我的 ViewModel 上的相应部分CreateAuthViewModel

public Authorization Authorization
{
    get
    {
       return this.authorization;
    }
    set
    {
       if (authorization != value)
       {
          this.authorization = value;
          NotifyOfPropertyChange(() => Authorization);                    
       }
    }
}

最后是我模型上的属性Authorization

public bool? SelfDirectedSupportYes
{
   get
   {
      return this.selfDirectedIndicator;
   }
   set
   {
      this.selfDirectedIndicator = value;
      this.OnPropertyChanged();
   }
}
4

2 回答 2

3

IsChecked也是 type 的Nullable<bool>,所以可以直接绑定“yes”单选按钮。如果您GroupName为两个单选按钮设置相同,则根本不需要绑定“否”单选按钮(因为选中“否”将取消选中“是”)

<telerik:Label Content="Self Directed Support" />
<StackPanel Orientation="Horizontal">
     <telerik:RadRadioButton x:Name="SelfDirectedSupportYes" GroupName="SelfDirectedSupportOption" Content="Yes" IsChecked="{Binding IsSDS}" />
     <telerik:RadRadioButton x:Name="SelfDirectedSupportNo" GroupName="SelfDirectedSupportOption" Content="No" />
</StackPanel>
于 2013-05-02T20:07:51.487 回答
1
    <telerik:RadRadioButton x:Name="SelfDirectedSupportYes" IsChecked="{Binding IsSDS}" Content="Yes" />

    <telerik:RadRadioButton x:Name="SelfDirectedSupportNo" IsChecked="{Binding IsSDS, Converter={StaticResource InverseBooleanConverter}}" Content="No" />






    [ValueConversion(typeof(bool), typeof(bool))]
       public class InverseBooleanConverter: IValueConverter
    {
       #region IValueConverter Members

         public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
       {
         if (targetType != typeof(bool))
            throw new InvalidOperationException("The target must be a boolean");

          return !(bool)value;
       }

        public object ConvertBack(object value, Type targetType, object parameter,
           System.Globalization.CultureInfo culture)
       {
           throw new NotSupportedException();
       }

        #endregion
     }
于 2013-05-02T19:49:54.133 回答