Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify an empty string as Target null value in xaml

Tags:

c#

xaml

From here https://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.targetnullvalue(v=vs.110).aspx,

It gives an example of specifying 'TargetNullValue':

<TextBox Width="150"
         Text="{Binding Source={StaticResource object2}, 
  Path=PropertyB, BindingGroupName=bindingGroup, 
  TargetNullValue=please enter a string}" />

My question is how can I specify an empty string for TargetNullValue?

I have tried "TargetNullValue= }" (there is a space between = and }, but that does not work, the target null value is null, instead of an empty string.

Thank you.

like image 208
n179911 Avatar asked Jul 20 '15 22:07

n179911


2 Answers

I don't know why the answer saying to use '' is deleted, but it works fine for me:

<TextBox Text="{Binding NumberOfCats, TargetNullValue=''}" />
like image 153
Simon_Weaver Avatar answered Oct 06 '22 16:10

Simon_Weaver


You can use x:Static in the xaml and define the string.empty there.

<TextBox Width="150"
         Text="{Binding Source={StaticResource object2}, 
  Path=PropertyB, BindingGroupName=bindingGroup, 
  TargetNullValue={x:Static system:String.Empty} }" />

You'll need to add the appropriate namespace to the xaml as needed. VS should do this for you though. Namespace required is

xmlns:system="clr-namespace:System;assembly=mscorlib"
like image 22
scartag Avatar answered Oct 06 '22 17:10

scartag