How to change an XAML glyph from C#?

Advertisements

I have an XAML label that shows an empty box. It’s using a glyph font-family similar to Microsoft’s MDL2 assets (except cross-platform).

<Label Content="&#xE739;" FontFamily="avares://HomeworkCalendar/Assets/Fonts#Symbols" PointerEnter="Check_OnPointerEnter" PointerLeave="Check_OnPointerLeave"/>

When the user hovers over the element I change it from a box to a checkbox.

private void Check_OnPointerEnter(object? sender, PointerEventArgs e) {
    var label = (Label)sender!;
    label.Content = "&#xE73A"; // Checked checkbox
}

private void Check_OnPointerLeave(object? sender, PointerEventArgs e) {
    var label = (Label)sender!;
    label.Content = "&#xE739"; // Unchecked checkbox
}

At program launch it displays the unchecked checkbox but the code-behind changes it to just the text &#xE73A and not a glyph. I know why this is but I can’t find anywhere how to parse it differently so it shows up correctly. Anyone know how I can have it parse correctly?

>Solution :

While &#x is used in XAML to denote that the next four characters of a string literal are a hex code, \x is used in C#.

XAML: &#xE739

C#: \xE739

Leave a ReplyCancel reply