2015-07-20 62 views
4

目標空值從這裏https://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.targetnullvalue(v=vs.110).aspx如何指定一個空字符串作爲在XAML

它給指定的「TargetNullValue」的例子:

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

我的問題是我怎麼可以指定一個空字符串TargetNullValue?

我已經試過"TargetNullValue= }"(有=和}之間的空間,但是,這並不可行,目標空值爲null,而不是一個空字符串。

謝謝。

回答

8

你可以在XAML中使用x:Static和定義的String.Empty那裏。

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

你需要根據需要適當的名稱空間添加到XAML。VS應該爲你雖然做到這一點。需要命名空間是

xmlns:system="clr-namespace:System;assembly=mscorlib" 
+0

我得到「在XML命名空間‘’未知類型」靜態HTTP:/ /schemas.microsoft.com/winfx/2006/xaml'' – n179911

+0

我在名稱空間 – n179911

+0

中添加了'xmlns:system =「using:System」我嘗試複製並粘貼您的名稱空間聲明,然後更改爲但我仍然收到相同的編譯器錯誤:XamlCompiler錯誤WMC0001:未知類型'XML'名稱空間中'靜態'http://schemas.microsoft.com/winfx/2006/x – n179911

1

我不知道爲什麼答案說使用''被刪除,但它爲我工作得很好:

<TextBox Text="{Binding NumberOfCats, TargetNullValue=''}" /> 
相關問題