5

I am trying to make a Grid with an Icon in it. I am storing the string representation of the icon in a database, and trying to display these icons in a GridHub by binding to the Icon property (a string) on my viewmodel.

When I use this xaml, I get the actual text displayed in the grid

<TextBlock Text="{Binding Icon}"
    FontFamily="Segoe UI Symbol"
    FontSize="100"
    TextAlignment="Center"
    VerticalAlignment="Top"
    Foreground="DarkGray"
    AutomationProperties.Name="Some Label"/>

But this displays the icon as expected

<TextBlock Text="&#xE1D4;"
    FontFamily="Segoe UI Symbol"
    FontSize="100"
    TextAlignment="Center"
    VerticalAlignment="Top"
    Foreground="DarkGray"
    AutomationProperties.Name="Some Label"/>

My Model looks like:

 public class PermissionGroup
{
    /// <summary>
    /// The unique identifier for the group.
    /// </summary>
    public string PermissionGroupId { get; set; }
    /// <summary>
    /// The name of the group
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// The page associated with the permission group
    /// </summary>
    public string PageName { get; set; }

    /// <summary>
    /// The icon that will be shown on the PermissionHub page
    /// </summary>
    public string Icon { get; set; }

    /// <summary>
    /// A description of the permissions in the group
    /// </summary>
    public string Description { get; set; }

    /// <summary>
    /// The permissions associated with this permission group
    /// </summary>
    public List<Permission> Permissions { get; set; } 
}

and my viewmodel just contains a list of these objects.

In my database I have tried storing the following:

  1. &#xE1D4;
  2. \uE1D4 (has to be escaped to get in the db)
  3. 57812 (the result of char.Parse("\uE1D4"))

and none of these has resulted in the icon being displayed correctly!

The output of first XAML example

Mike
  • 1,718
  • 3
  • 30
  • 58

1 Answers1

6

You have to decode the html encoded string. For instance you can try this very rough example:

public class ViewModel
    {
        public PermissionGroup PG {get; set;}
        public ViewModel()
        {
            PG = new PermissionGroup();
            PG.Icon= System.Net.WebUtility.HtmlDecode("&#xE1D4;");
        }
    }




public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            this.DataContext = new ViewModel();
        }
    }


<TextBlock Text="{Binding Path=PG.Icon}" FontFamily="Segoe UI Symbol"/>

or

<Style x:Key="Segoe">
    <Setter x:Name="Segoe" Property="TextElement.FontFamily" Value="Resources/#Segoe UI Symbol" />
</Style>

<TextBlock Text="{Binding PG.Icon}"
            Style="{DynamicResource Segoe}"

A font viewer is available here, in case you need one.

EDIT: anyway the unicode escape sequence should work better than the html encoding, that's why I'm suggesting to double check with a font viewer and to link the font ttf as a resource. You can also add a button to analyse the unicode conversion of your xaml text, when directly entered without binding. Something like this

private void Button_Click(object sender, RoutedEventArgs e)
{
    byte[] stringBytes = Encoding.Unicode.GetBytes(txname.Text);
    char[] stringChars = Encoding.Unicode.GetChars(stringBytes);
    StringBuilder builder = new StringBuilder();
    Array.ForEach<char>(stringChars, c => builder.AppendFormat("\\u{0:X}", (int)c));
    Debug.WriteLine(builder);
} 
  • Thanks Giulio, I will give this a try. I am not sure HTML decode is the right thing to do since I am not creating HTML (its XAML), but I will see if there is a decoding mechanism I can use. Thanks. – Mike Feb 22 '15 at 23:21
  • I've tried those lines of code before posting them, of course :-) Besides that specific string and regarding your comment/doubt, now I've found also this external link from msdn (about xaml) that appears to confirm my hint: https://social.msdn.microsoft.com/Forums/silverlight/en-US/ee719905-bb65-41f1-9885-ab70775d71c5/textblock-encoded-text –  Feb 23 '15 at 03:00
  • You may have a look also at [this](http://stackoverflow.com/a/6856459/4569980) answer about including an external font in xaml –  Feb 23 '15 at 05:56