Reputation: 2470
For example, let's say i want to bind following value to the Text property of an TextBlock: & amp; what has to be showed as a &.
How is the binding syntax for it?
<TextBlock Text="{Binding Foo}" HorizontalAlignment="Left" />
class MyViewModel : BaseViewModel {
public string Foo {get; set;}
public MyViewModel { Foo = "&" }
}
This is showing "& amp;" and not "&".
Note that the char & is choosen arbitrary and the question is about any char what you can not type from the keyboard directly like &. e.g. &8730;
Upvotes: 1
Views: 889
Reputation: 37066
The C# parser isn't an XML parser, so it has no idea that &
is anything special. The Text property of the control isn't an XML parser either, so it just displays the five characters you gave it.
So give it the actual character. If you're doing that in a C# literal string, encode it for the C# parser, because that's the parser that's parsing it -- in this specific case, just give it a literal ampersand, which in a C# literal string requires no escaping. For \x8730
, just give it "\x8730"
.
public MyViewModel { Foo = "&" }
It's not WPF controls that require XML encoding; it's the XML parser that requires it.
Upvotes: 2