Search code examples
c#wpfradio-buttonivalueconverter

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


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

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

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

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

Solution

  • @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.

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