3

我有一个 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>

布尔到字符串转换器

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

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

编辑:添加了 Primary 来自的类。

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

编辑 2:添加 EntityRefs 属性

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

1 回答 1

1

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

 <DataTemplate>
   <RadioButton GroupName="Prime" IsChecked="{Binding LUREFFORMAT.PRIMARY, Mode=TwoWay, Converter={StaticResource BoolToPrimaryConverter}, UpdateSourceTrigger=PropertyChanged}" />
 </DataTemplate>
于 2012-10-04T16:09:51.123 回答