I would like to create a control to behave like this:
<controls:FontIcon Code=""
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=""
IconStyle="Regular" />
<TextBlock Text="{Binding ElementName=FontIcon, Path=FontFamily}" />
</StackPanel>