我正在尝试在DataGridTextColumn中设置默认值,以防绑定属性给出Null值,类似于sql server中的if语句...
因此,我进行了研究,并找到了TargetNullValue属性,可以在列为空时放置默认值。
但是它并没有按照我的方式工作。抛出了下一个错误
System.Windows.Markup.XamlParseException:“不能在类型为'Binding'的'TargetNullValue'属性上设置'Binding'。只能在DependencyObject的DependencyProperty上设置'Binding'。”
以下是我的XAML代码示例。
实际上,它是使用数据源绑定到EntityFramework...
因此,我进行了研究,并找到了TargetNullValue属性,可以在列为空时放置默认值。
但是它并没有按照我的方式工作。抛出了下一个错误
System.Windows.Markup.XamlParseException:“不能在类型为'Binding'的'TargetNullValue'属性上设置'Binding'。只能在DependencyObject的DependencyProperty上设置'Binding'。”
以下是我的XAML代码示例。
<DataGrid x:Name="proveedorDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding IsAsync=True}" Margin="15,50,25,70" RowDetailsVisibilityMode="VisibleWhenSelected"
SelectionMode="Single" IsReadOnly="True" CanUserAddRows="False" CanUserResizeRows="False" CanUserDeleteRows="False" PreviewKeyDown="ProveedorDataGrid_OnPreviewKeyDown">
<DataGrid.Columns>
<DataGridTextColumn x:Name="municipioColumn" Binding="{Binding Municipio, TargetNullValue={Binding CCodigoPostal.Municipio}}" Header="Municipio" Width="Auto" />
</DataGrid.Columns>
</DataGrid>
当前资源情况下
<Window.Resources>
<CollectionViewSource x:Key="proveedorViewSource" d:DesignSource="{d:DesignInstance {x:Type Core:Proveedor}, CreateList=True}"/>
</Window.Resources>
实际上,它是使用数据源绑定到EntityFramework...
非常感谢您的帮助!
*更新后的CLASS
public class Proveedor
{
public Proveedor()
{
ValeVehiculoCombustibles = new HashSet<ValeVehiculoCombustible>();
FacturaProveedors = new HashSet<FacturaProveedor>();
EntradaBasculas = new HashSet<EntradaBascula>();
}
public Guid? Id_Proveedor { get; set; }
public string Codigo { get; set; }
public string RazonSocial { get; set; }
public string Calle { get; set; }
public string Colonia { get; set; }
public virtual C_CodigoPostal CCodigoPostal { get; set; }
public int CCodigoPostalId { get; set; }
public string Telefonos { get; set; }
public string RFC { get; set; }
public virtual CuentaContable CuentaContable { get; set; }
public Guid? CuentaContableId { get; set; }
public short DiasCred { get; set; }
public decimal SaldoMN { get; set; }
public decimal AnticipoMN { get; set; }
public decimal SaldoDlls { get; set; }
public decimal AnticipoDlls { get; set; }
public string Contacto { get; set; }
public string Email { get; set; }
public string Municipio { get; set; }
public string Estado { get; set; }
public string Ciudad { get; set; }
public bool Estatus { get; set; }
public DateTime Actualizado { get; set; }
public virtual ICollection<ValeVehiculoCombustible> ValeVehiculoCombustibles { get; set; }
public virtual ICollection<FacturaProveedor> FacturaProveedors { get; set; }
public virtual ICollection<EntradaBascula> EntradaBasculas { get; set; }
}
** 和他的配置
public class ProveedorConfiguration : EntityTypeConfiguration<Proveedor>
{
public ProveedorConfiguration()
{
ToTable("Proveedor");
//PK
HasKey(p => p.Id_Proveedor);
Property(p => p.Id_Proveedor)
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(p => p.Codigo)
.IsRequired()
.HasColumnType("varchar")
.HasMaxLength(6);
HasIndex(p => p.Codigo)
.IsUnique();
Property(p => p.RazonSocial)
.IsRequired()
.HasColumnType("varchar")
.HasMaxLength(60);
Property(p => p.Calle)
.IsRequired()
.HasColumnType("varchar")
.HasMaxLength(40);
Property(p => p.Colonia)
.HasColumnType("varchar")
.HasMaxLength(30);
Property(p => p.CCodigoPostalId)
.IsRequired()
.HasColumnType("int");
Property(p => p.Telefonos)
.HasColumnType("varchar")
.HasMaxLength(30);
Property(p => p.RFC)
.HasColumnType("varchar")
.HasMaxLength(13);
HasIndex(p => p.RFC)
.IsUnique();
Property(p => p.CuentaContableId)
.IsRequired();
Property(p => p.DiasCred)
.IsRequired()
.HasColumnType("smallint");
Property(p => p.SaldoMN)
.IsRequired()
.HasColumnType("decimal")
.HasPrecision(10, 2);
Property(p => p.AnticipoMN)
.IsRequired()
.HasColumnType("decimal")
.HasPrecision(10, 2);
Property(p => p.SaldoDlls)
.IsRequired()
.HasColumnType("decimal")
.HasPrecision(8, 2);
Property(p => p.AnticipoDlls)
.IsRequired()
.HasColumnType("decimal")
.HasPrecision(8, 2);
Property(p => p.Contacto)
.HasColumnType("varchar")
.HasMaxLength(60);
Property(p => p.Email)
.HasColumnType("varchar")
.HasMaxLength(90);
Property(p => p.Municipio)
.HasColumnType("varchar")
.HasMaxLength(50);
Property(p => p.Estado)
.HasColumnType("varchar")
.HasMaxLength(31);
Property(p => p.Ciudad)
.HasColumnType("varchar")
.HasMaxLength(45);
Property(p => p.Estatus)
.IsRequired();
//Relationships
//FK Proveedor -> ValeVehiculoCombustible
HasMany(p => p.ValeVehiculoCombustibles)
.WithOptional(v => v.Proveedor)
.HasForeignKey(v => v.ProveedorId)
.WillCascadeOnDelete(false);
//PK Proveedor -> FacturaProveedor
HasMany(p => p.FacturaProveedors)
.WithRequired(f => f.Proveedor)
.HasForeignKey(f => f.ProveedorId)
.WillCascadeOnDelete(false);
//FK Proveedor -> EntradaBasucla
HasMany(p => p.EntradaBasculas)
.WithRequired(e => e.Proveedor)
.HasForeignKey(e => e.ProveedorId)
.WillCascadeOnDelete(false);
}
}
最后,在调试模式下,这是查询的返回结果...