Set value to null in WPF binding

Shimmy Weitzhandler picture Shimmy Weitzhandler · Dec 13, 2009 · Viewed 47.6k times · Source

please take a look at the following line

<TextBox Text="{Binding Price}"/>

This Price property from above is a Decimal? (Nullable decimal).

I want that if user deletes the content of the textbox (i.e. enters empty string, it should automatcally update source with null (Nothing in VB).

Any ideas on how I can do it 'Xamly'?

Answer

Shimmy Weitzhandler picture Shimmy Weitzhandler · Dec 13, 2009

I am using .NET 3.5 SP1 so it's very simple:

<TextBox Text="{Binding Price, TargetNullValue=''}"/>

Which stands for (thanks Gregor for your comment):

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

sys is the imported xml namespace for System in mscorlib:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Hope that helped.