简体   繁体   中英

Creating a reusable IconFont Control

I would like to create a control to behave like this:

<controls:FontIcon Code="&#xf0c7;"
                   IconStyle="Solid"
                   FontSize="25" />

Sadly I cannot seem to switch the FontFamily based on IconStyle .

public class FontIcon : TextBlock
{
    public static readonly DependencyProperty IconStyleProperty =
        DependencyProperty.Register(
            "IconStyle",
            typeof(Style),
            typeof(FontIcon),
            new PropertyMetadata(Controls.Style.Regular));

    public static readonly DependencyProperty CodeProperty =
        DependencyProperty.Register(
            "Code",
            typeof(string),
            typeof(FontIcon),
            new PropertyMetadata(null));

    public string Code
    {
        get { return (string) GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public Style IconStyle
    {
        get { return (Style) GetValue(IconStyleProperty); }
        set
        {
            SetValue(IconStyleProperty, value);
            SwitchFontFamily(value);
        }
    }

    private void SwitchFontFamily(Style style)
    {
        switch (style)
        {
            case Controls.Style.Regular:
                SetValue(FontFamilyProperty, new FontFamily(new Uri(@"pack://application:,,,//<MyClassLibsNamespace>;component/Fonts/Font Awesome 5 Pro-Regular-400.otf"), "Font Awesome 5 Pro Regular"));
                break;
            case Controls.Style.Solid:
                SetValue(FontFamilyProperty, new FontFamily(new Uri(@"pack://application:,,,//<MyClassLibsNamespace>;component/Fonts/Font Awesome 5 Pro-Solid-900.otf"), "Font Awesome 5 Pro Solid"));
                break;
            case Controls.Style.Light:
                SetValue(FontFamilyProperty, new FontFamily(new Uri(@"pack://application:,,,/<MyClassLibsNamespace>;component/Fonts/Font Awesome 5 Pro-Light-300.otf"), "Font Awesome 5 Pro Light"));
                break;
        }
    }
}

public enum Style
{
    Regular,
    Solid,
    Light
}

Why isn't it displaying my Icon ?

Edit 1

private void SwitchFontFamily(Style style)
    {
        switch (style)
        {

            case Controls.Style.Regular:
                FontFamily fromLibs = new FontFamily(new Uri("pack://application:,,,/UIToolsWPF;component/"), "./Fonts/#Font Awesome 5 Pro Regular");
                FontFamily = fromLibs;
                break;
            //case Controls.Style.Solid:
            //    FontFamily = new FontFamily(new Uri(@"pack://application:,,,/UIToolsWPF;component/Fonts/Font Awesome 5 Pro-Solid-900.otf"), "Font Awesome 5 Pro Solid");
            //    break;
            //case Controls.Style.Light:
            //    FontFamily = new FontFamily(new Uri(@"pack://application:,,,/UIToolsWPF;component/Fonts/Font Awesome 5 Pro-Light-300.otf"), "Font Awesome 5 Pro Light");
            //    break;
        }
    }


<StackPanel VerticalAlignment="Center"
            HorizontalAlignment="Center">
    <controls:FontIcon HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       FontSize="25"
                       x:Name="FontIcon"
                       Text="&#xf039;"
                       IconStyle="Regular" />
     <TextBlock Text="{Binding ElementName=FontIcon, Path=FontFamily}" />
</StackPanel>

在此处输入图片说明

In order to make this work I had to use the pattern described in here .

// The font resource reference includes the base URI reference (application directory level),
// and a relative URI reference.
myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./resources/#Pericles Light");

I used:

string fontRoot = "pack://application:,,,/<MyReferencedAssembly>;component/";
FontFamily = new FontFamily(new Uri(fontRoot), "./Fonts/#Font Awesome 5 Pro Regular");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM