【问题标题】:Binding RadioButton to Enum将 RadioButton 绑定到枚举
【发布时间】:2015-10-17 17:49:39
【问题描述】:

问题: 使用参数化转换器将 Enum 类型的属性绑定到 RadioButtons。没有抛出异常,Radiobutton 可能有验证问题(不确定)。测试时会显示 RadioButtons 周围的红色框。

信息: 试图使用给出的解决方案 How to bind RadioButtons to an enum?

我有一个这样的枚举:

namespace crmVerwaltungstools.Models
{
   public enum CrmSystemType
   {
     Training = 0,
     Live = 1
   }
}

BooleanToEnumConverter:

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(true) ? (CrmSystemType)parameter : Binding.DoNothing;
    }

在我的窗口内:

xmlns:models="clr-namespace:crmVerwaltungstool.Models"

     <StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Orientation="Horizontal">
          <StackPanel.Resources>
                <converter:RadioButtonIsCheckedToCrmSystemTypeConverter x:Key="RbIsCheckedToCrmSystemTypeConverter" />
          </StackPanel.Resources>

          <RadioButton Content="Schulungs-System" GroupName="rbg_SelectSystem"
                                 IsChecked="{Binding Path=SystemType, Converter={StaticResource RbIsCheckedToCrmSystemTypeConverter},
                                 ConverterParameter={x:Static models:CrmSystemType.Training}}"/>
          <RadioButton Content="Live-System" GroupName="rbg_SelectSystem"
                                 IsChecked="{Binding Path=SystemType, Converter={StaticResource RbIsCheckedToCrmSystemTypeConverter},
                                 ConverterParameter={x:Static models:CrmSystemType.Live}}"/>
      </StackPanel>

看不到任何错误。 (可能是今天看到的代码行数太多了……)

感谢您的帮助!!

【问题讨论】:

    标签: c# wpf data-binding enums radio-button


    【解决方案1】:

    问题解决了。

    我在视图模型中发现了一小段旧代码,我试图将我的枚举定义为内部类。

    所以,基本上,我的程序对使用哪个枚举感到困惑 - viewmodel 内的内部类或模型文件夹内的外部类。

    删除内部枚举后,一切正常。

    【讨论】:

      【解决方案2】:

      首先你需要检查你的转换器值不为空:

      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              if (value == null)
              {
                  return false;
              }
              return value.Equals(parameter);
          }
      

      在 ConvertBack 方法中也这样做。

      其次,编写你的 xaml 类似的东西:

      <StackPanel>
          <StackPanel.Resources>          
              <local:EnumToBooleanConverter x:Key="EnumToBooleanConverter" />          
          </StackPanel.Resources>
          <RadioButton IsChecked="{Binding Path=YourEnumProperty, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:YourEnumType.Enum1}}" />
          <RadioButton IsChecked="{Binding Path=YourEnumProperty, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:YourEnumType.Enum2}}" />
      </StackPanel>
      

      【讨论】:

      • 早上好,还是有这个问题。添加了value==null 检查。在测试时,从来没有看到它变成了“真的”。也许是因为枚举的默认值是该枚举的 index[0] 处的值,在我的情况下,默认值将是“训练”。实际上Stackpanel.Resourcespart 已经存在,忘记输入我的问题的代码。我将编辑我的问题并添加缺失的部分。感谢您的帮助!
      • 你的意思是CrmSystemType?那是我自己创建的一个 Enum-Type,用于选择我想连接到我们的哪个 Crm-Server。实时或培训服务器。
      • 这是我的财产的名称。 private CrmSystemType systemTypepublic CrmSystemType SystemType { get { return systemType; } set { systemType = value; OnPropertyChanged("SystemType"); } }
      • 好的,在一个单独的项目中明确测试这个功能,它工作得很好。就像我在“信息”下提到的解决方案一样。所以我的代码里面一定有一些问题。好吧,现在我必须在我的代码中挖掘它 xD 但是感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 2011-11-01
      相关资源
      最近更新 更多