可空数据库属性,但texbox仍然显示内容时删除红色边框 [英] Nullable database property but texbox still shows red border when content deleted

查看:147
本文介绍了可空数据库属性,但texbox仍然显示内容时删除红色边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我绑定一个WPF文本框的实体框架特性如下:

Hi I am binding a WPF textbox to an Entity Framework property as follows:

<TextBox Grid.Column="1" Grid.Row="0" Margin="5,2" 
         Text="{Binding Path=MyEntityObject.SizeLower, Mode=TwoWay}" />

它结合精致的财产,当我改变它,因为预计其保存到数据库中。但是,如果我删除文本框的内容,我得到的红色错误边框周围。我没有在任何地方验证,所以我猜texbox抱怨不是可为空值。但实际上这个属性在DB是空的,因此我不明白为什么它会报错。

It binds fine to the property and when I change it, it saves to the DB as expected. But if I delete the content of the Textbox I get the red error border around it. I dont have any validator in place so I am guessing the texbox is complaining about the value not being nullable. But in fact this property in the DB is nullable, so I cannot understand why it would error.

本系统生成EF属性定义如下:

The system generated EF property definition is as follows:

<EdmScalarPropertyAttribute(EntityKeyProperty:=false, IsNullable:=true)>
<DataMemberAttribute()>
Public Property SizeLower() As Nullable(Of Global.System.Int64)
    Get
        Return _SizeLower
    End Get
    Set
        OnSizeLowerChanging(value)
        ReportPropertyChanging("SizeLower")
        _SizeLower = StructuralObject.SetValidValue(value)
        ReportPropertyChanged("SizeLower")
        OnSizeLowerChanged()
    End Set
End Property

Private _SizeLower As Nullable(Of Global.System.Int64)

有我丢失的东西?我想绑定系统能够确定是否财产是可为空,并允许空值如果是这样?

Is there something I am missing? I thought the binding system was able to determine if a property was nullable and allow nulls if so?

我怎么能看到的错误是什么?悬停似乎没有这样的伎俩。

How can I see what the error is? Hovering doesnt seem to do the trick.

感谢您的任何建议。

===================================
其他信息

=================================== ADDITIONAL INFO

如果我选择全部删除,然后更改焦点,会出现验证框。之前和之后这里有一个抓屏。我也已经证实,我可以手动把空值在数据库中的绑定属性所以那不是问题。

If I select all and delete, then change focus, the validation box appears. Here's a screencapture before and after. Also I have confirmed that I can manually put NULLs in the database for the bound properties so thats not the problem.

拒绝。试图把这里的图片,但我没有10分...!
下面是一个异地而不是链接:点击此处

DENIED. Tried to put picture here but I dont have 10 points...! Here is an offsite link instead: CLICK HERE

推荐答案

您应该在 TargetNullValue 属性添加到您的绑定:

You should add the TargetNullValue property to your binding:

<TextBox Grid.Column="1" Grid.Row="0" Margin="5,2" 
         Text="{Binding Path=MyEntityObject.SizeLower, 
         Mode=TwoWay, 
         TargetNullValue=''}" />

这告诉绑定设置时,对待 MyEntityObject.SizeLower 空值的String.Empty用于显示,作为的String.Empty空。

This tells the binding to treat null values in MyEntityObject.SizeLower as string.empty for display, and string.empty as null when setting.

这篇关于可空数据库属性,但texbox仍然显示内容时删除红色边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆