我有一个 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");
}
}