如何为 WPF RadioButton 创建布尔到字符串转换器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12730340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:39:20  来源:igfitidea点击:

How do I create a Boolean to String Converter for a WPF RadioButton?

c#wpfradio-buttonivalueconverter

提问by Bob.

I have a DataGridColumn which represents whether an entry is the primary display value, but the value is stored in the database as "Y" or "N".

我有一个 DataGridColumn,它表示一个条目是否是主要显示值,但该值在数据库中存储为“Y”或“N”。

 <Window.Resources>
      <local:BoolToPrimaryConverter x:Key="BoolToPrimaryConverter" />
 </Window.Resources>
 <DataGrid Name="NamingDatagrid" AutoGenerateColumns="False" ItemsSource="{Binding EntityReferences, Mode=TwoWay}"
              CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Convention" Binding="{Binding ReferenceType}"/>
            <DataGridTextColumn Header="Value" Binding="{Binding ReferenceValue}" />
            <DataGridTemplateColumn Header="Primary" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <RadioButton GroupName="Prime" IsChecked="{Binding Primary, Mode=TwoWay, Converter={StaticResource BoolToPrimaryConverter}, UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

Boolean to String Converter

布尔到字符串转换器

 class BoolToPrimaryConverter : IValueConverter {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
           return ((value as string).Equals("Y")) ? true : false;
      }

      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
           return (bool)value ? "Y" : "N";
      }
 }

Even though the Converter is set, it is never called (checked with debugger). Is there something I am missing that I need to get this working?

即使设置了 Converter,它也不会被调用(用调试器检查)。有什么我需要让它工作的东西吗?

Edit: Added the Class where Primary comes from.

编辑:添加了主要来自的类。

 [Serializable]
 [EdmEntityType(NamespaceName = "cContainer", Name = "LUREFFORMAT")]
 [DataContract(IsReference = true)]
 public class LUREFFORMAT : EntityObject {
      public LUREFFORMAT();

      [DataMember]
      [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)]
      public short DISPLAYORDER { get; set; }
      [DataMember]
      [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)]
      public short ENABLED { get; set; }
      [DataMember]
      [EdmScalarProperty(EntityKeyProperty = true, IsNullable = false)]
      public long ID { get; set; }
      [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)]
      [DataMember]
      public string ISUNIQUE { get; set; }
      [DataMember]
      [XmlIgnore]
      [EdmRelationshipNavigationProperty("cContainer", "FK_REFS_REFFORMATID", "REFS")]
      [SoapIgnore]
      public EntityCollection<PLATFORMREFS> REFS { get; set; }
      [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)]
      [DataMember]
      public string PRIMARY { get; set; }
      [DataMember]
      [EdmScalarProperty(EntityKeyProperty = false, IsNullable = false)]
      public string REFFORMAT { get; set; }

      public static LUREFFORMAT CreateLUREFFORMAT(long id, string rEFFORMAT, string pRIMARY, string iSUNIQUE, short dISPLAYORDER, short eNABLED);
 }

Edit 2: Added EntityRefs property

编辑 2:添加 EntityRefs 属性

 public EntityCollection<PLATFORMREFS> EntityRefs {
      get {
           return entityRefs;
      }
      set {
           entityRefs = value;
           OnPropertyChanged("EntityRefs");
      }
 }

采纳答案by kmatyaszek

@Bob so if it the same, change binding from Primary to upper case PRIMARY, and also PRIMARY is a value in LUREFFORMAT and cannot be accessed directly, just point from the LUREFFORMAT class.

@Bob 因此,如果相同,请将绑定从 Primary 更改为大写 PRIMARY,而且 PRIMARY 是 LUREFFORMAT 中的一个值,无法直接访问,只需从 LUREFFORMAT 类中指向即可。

 <DataTemplate>
   <RadioButton GroupName="Prime" IsChecked="{Binding LUREFFORMAT.PRIMARY, Mode=TwoWay, Converter={StaticResource BoolToPrimaryConverter}, UpdateSourceTrigger=PropertyChanged}" />
 </DataTemplate>