问题
I have an TextBlock and I am trying to bind the text to a font-awesome character to use as a checkbox. I have a property that is returning a character string, but it seems that something is converting it to a string instead of a character.
This works and draws a checkbox:
<TextBlock Text="" FontFamily="/Resources/fontawesome-webfont.ttf#FontAwesome" />
This combination does not:
public class myObject
{
public string MyString
{
get { return "" }
}
}
<TextBlock Text{Binding MyString} FontFamily="/Resources/fontawesome-webfont.ttf#FontAwesome" />
Instead, it draws the actual string (with the #xf096; et al) as if it were escaped.
Any ideas why or how to make this return the unquoted character?
Thanks!
回答1:
It looks like your goal can be achieved using unicode escape sequences:
public class myObject
{
public string MyString
{
get { return "\uf096"; }
}
}
<TextBlock Text="{Binding MyString}" FontFamily="../Resources/fontawesome-webfont.ttf#FontAwesome" />
Then the string is processed even when provided by Binding. I tested it on desktop but I think this should work likewise on phone version.
回答2:
Well, I'm not sure if this is the reason or not, but your TextBlock should read Text="{Binding MyString}"
(you're missing the quote and equals sign) and you should also make sure the DataContext for the page/control/whatever is set to an instance of your MyObject class so it can be picked up. Finally, your property probably needs to implement INotifyPropertyChanged if you ever intend to update the binding.
来源:https://stackoverflow.com/questions/19577489/binding-font-awesome-character-in-xaml-to-text