binding font-awesome character in XAML to Text

霸气de小男生 提交于 2019-12-11 01:38:01

问题


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="&#xf096;" FontFamily="/Resources/fontawesome-webfont.ttf#FontAwesome" />

This combination does not:

public class myObject 
{
    public string MyString
    {
        get { return "&#xf096;" }
    }
}

<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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!