XAML nested binding StringFormat
I am trying to display an adress label. what i want is if AddresssLine2 is no开发者_StackOverflow社区t an empty string (its never null) it should display it then a newline (I'm using VB, so its 
), otherwise just display AddressLine2, which is an empty string, so in escense it is ignored. however, the StringFormat of the inner binding of AddressLine2 is completely ignored. it just displays the value of AddressLine2 and ignores the StringFormat. I even tried putting a constant only into StringFormat (StringFormat=" hi") but it ignored it and bound directly to AddressLine2. here is my markup.
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} 
{1}
{2} {3}, {4} {5}">
<Binding Path="FullName" />
<Binding Path="AddressLine1" />
<Binding Path="AddressLine2" StringFormat="{}{0}
" />
<Binding Path="City" />
<Binding Path="State" />
<Binding Path="ZipCode" />
</MultiBinding>
</TextBlock.Text>
my question is: why does it ignore the inner StringFormat (I tried all different combinations; "'{0}
'"
, "'{}{0}
'"
, " {0}
"
, none worked)?
Also, is there a better way to do this (in XAML, I dont want a converter or any code behind)?
MSDN says the StringFormat property gets or sets a string that specifies how to format the binding if it displays the bound value as a string.
Since you are using its value, and not displaying it in the multibinding, it will use the value in your MultiBinding StringFormat.
In short: StringFormat on Bindings in MultiBindings will be ignored so to speak.
When you use a MultiBinding, the StringFormat property applies only when it is set on the MultiBinding. The value of StringFormat that is set on any child Binding objects is ignored. The number of parameters in a composite string format cannot exceed the number of child Binding objects in the MultiBinding.
Workaround: expand the MultiBinding StringFormat:
StringFormat="{}{0} 
{1}
{2}
{3}, {4} {5}"
But I guess you could figure that one out as well ;)
精彩评论