Skip to content

Instantly share code, notes, and snippets.

@punker76
Forked from darxis/PackIconControl.cs
Created April 21, 2019 08:36
Show Gist options
  • Save punker76/9316524fb513b5135cffd2d869ba73b4 to your computer and use it in GitHub Desktop.
Save punker76/9316524fb513b5135cffd2d869ba73b4 to your computer and use it in GitHub Desktop.
public class PackIconControl : ContentControl
{
public PackIconControl()
{
SizeChanged += OnSizeChanged;
}
public static readonly DependencyProperty PackIconKindProperty =
DependencyProperty.Register(nameof(PackIconKind), typeof(object), typeof(PackIconControl),
new PropertyMetadata(default(object), PackIconKindPropertyChangedCallback));
private static void PackIconKindPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
PackIconBase newContent;
switch (dependencyPropertyChangedEventArgs.NewValue)
{
case PackIconEntypoKind kind:
newContent = new PackIconEntypo { Kind = kind };
break;
case PackIconFontAwesomeKind kind:
newContent = new PackIconFontAwesome { Kind = kind };
break;
case PackIconMaterialKind kind:
newContent = new PackIconMaterial { Kind = kind };
break;
case PackIconMaterialLightKind kind:
newContent = new PackIconMaterialLight { Kind = kind };
break;
case PackIconModernKind kind:
newContent = new PackIconModern { Kind = kind };
break;
case PackIconOcticonsKind kind:
newContent = new PackIconOcticons { Kind = kind };
break;
case PackIconSimpleIconsKind kind:
newContent = new PackIconSimpleIcons { Kind = kind };
break;
default:
newContent = null;
break;
}
var control = (PackIconControl)dependencyObject;
if (newContent != null)
{
newContent.Width = control.Width;
newContent.Height = control.Height;
}
control.Content = newContent;
}
public object PackIconKind
{
get => GetValue(PackIconKindProperty);
set => SetValue(PackIconKindProperty, value);
}
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
if (Content is PackIconBase content)
{
content.Width = Width;
content.Height = Height;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment