-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Expand file tree
/
Copy pathIconLabelControl.xaml.cs
More file actions
89 lines (75 loc) · 2.72 KB
/
IconLabelControl.xaml.cs
File metadata and controls
89 lines (75 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using KeyboardManagerEditorUI.Helpers;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace KeyboardManagerEditorUI.Controls
{
[TemplatePart(Name = TypeIconPart, Type = typeof(FontIcon))]
[TemplatePart(Name = LabelTextPart, Type = typeof(TextBlock))]
public sealed partial class IconLabelControl : Control
{
private const string TypeIconPart = "TypeIcon";
private const string LabelTextPart = "LabelText";
private FontIcon? _typeIcon;
private TextBlock? _labelText;
public static readonly DependencyProperty ActionTypeProperty =
DependencyProperty.Register(
nameof(ActionType),
typeof(ActionType),
typeof(IconLabelControl),
new PropertyMetadata(ActionType.Text, OnActionTypeChanged));
public static readonly DependencyProperty LabelProperty =
DependencyProperty.Register(
nameof(Label),
typeof(string),
typeof(IconLabelControl),
new PropertyMetadata(string.Empty));
public ActionType ActionType
{
get => (ActionType)GetValue(ActionTypeProperty);
set => SetValue(ActionTypeProperty, value);
}
public string Label
{
get => (string)GetValue(LabelProperty);
set => SetValue(LabelProperty, value);
}
public IconLabelControl()
{
this.DefaultStyleKey = typeof(IconLabelControl);
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
_typeIcon = GetTemplateChild(TypeIconPart) as FontIcon;
_labelText = GetTemplateChild(LabelTextPart) as TextBlock;
UpdateIcon();
}
private static void OnActionTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is IconLabelControl control)
{
control.UpdateIcon();
}
}
private void UpdateIcon()
{
if (_typeIcon == null)
{
return;
}
_typeIcon.Glyph = ActionType switch
{
ActionType.Program => "\uECAA",
ActionType.Text => "\uE8D2",
ActionType.Shortcut => "\uEDA7",
ActionType.MouseClick => "\uE962",
ActionType.Url => "\uE774",
ActionType.Expand => "\uE8C8",
_ => "\uE8A5",
};
}
}
}