Nullable database property but texbox still shows red border when content deleted

倖福魔咒の 提交于 2019-12-22 09:39:13

问题


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

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.

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.

Thanks for any advice.

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

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


回答1:


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=''}" />

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



来源:https://stackoverflow.com/questions/4322045/nullable-database-property-but-texbox-still-shows-red-border-when-content-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!