Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 574
TripleAntigen Avatar asked Dec 01 '22 04:12

TripleAntigen


1 Answers

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.

like image 163
Philip Rieck Avatar answered Dec 04 '22 10:12

Philip Rieck