views:

309

answers:

2

I am having difficulty setting an empty TextBox to null on an nullable DB Field.

XAML

<y:TextBox Text="{Binding Year1Cost, Mode=TwoWay,  
     UpdateSourceTrigger=PropertyChanged,   
     NotifyOnValidationError=True,   
     ValidatesOnDataErrors=True,   
     ValidatesOnExceptions=True,  
     StringFormat=\{0:c\}}" Grid.Row="3" Grid.Column="1" />

When I enter any value into it the binding is fine and the entered value is passed
When I leave a null value alone a null is passed
If I delete the value from the TextBox the value passed is the Original value of the Textbox and the UI is not notified of the change Grrrrrrrrrrrrrrrr

I have spent a long time checking out options nothing short of putting code behind the OnTextChanged of every nullable field I cannot see the efficiency in doing this.

Thanks in advance:

ps. Have looked at TargetNullValue to no success

Visual Studio 2008 - SP1 - .Net 3.5

A: 

Consider using a value converter. You should be able to implement the ConvertBack method to translate empty strings into nulls.

itowlson
Had to convert to a space, as an empty string did not trigger property changed event for some reason. Thank you for your help, I will implement on all required places.
Traci
+1  A: 

Set the property TargetNullValue of the binding to String.Empty:

<TextBox Text="{Binding TargetNullValue={x:Static sys:String.Empty}}"/>

I tried it and it works for me.

And if I am not wrong (please forgive me if I am), you should set the property StringFormat like this:

StringFormat={}{0:C}

This is even probably the reason of the exception you got.

Shimmy
xmlns:sys="clr-namespace:System;assembly=mscorlib" I tried this but keep getting Error 24 Could not load type 'Microsoft.Windows.Design.Metadata.ProvideMetadataAttribute' from assembly 'Microsoft.Windows.Design.Extensibility, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Line '1' Position '6680'.
Traci
I donno.But if I would be you, I would try just for a test to have a TextBox without the other definitions; just binding and TargetNullValue.This way can help you find the source of the exception.
Shimmy
Tried removing the StringFormat all together and I am still getting the same meassage. --- <TextBox Text="{Binding Year1Cost, TargetNullValue={x:Static sys:String.Empty}}" /> ---- That is all I have and still get the above exception
Traci
I honestly donno :( I wish I could help you
Shimmy