开发者

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

开发者 https://www.devze.com 2023-01-27 13:50 出处:网络
Hi I am binding a WPF textbox to an Entity Framework property as follows: <TextBox Grid.Column=\"1\" Grid.Row=\"0\" Margin=\"5,2\"

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号