35
|
1 using System;
|
|
2 using System.Windows;
|
|
3 using System.Windows.Controls;
|
|
4 using System.Windows.Input;
|
|
5 using System.Windows.Media;
|
|
6 using System.Windows.Media.Imaging;
|
|
7
|
|
8 namespace Common.Controls
|
|
9 {
|
|
10 [TemplatePart(Name="PART_DISPLAY_ICON", Type=typeof(Image))]
|
|
11 [TemplatePart(Name = "PART_DISPLAY_COUNT_CONTAINER", Type = typeof(TextBlock))]
|
|
12 [TemplatePart(Name = "PART_DISPLAY_TITLE_CONTAINER", Type = typeof(TextBlock))]
|
|
13 public class MetroTile : UserControl
|
|
14 {
|
|
15
|
|
16 #region Constructor
|
|
17
|
|
18 static MetroTile()
|
|
19 {
|
|
20 DefaultStyleKeyProperty.OverrideMetadata(typeof(MetroTile),
|
|
21 new FrameworkPropertyMetadata(typeof(MetroTile)));
|
|
22
|
|
23 CommandManager.RegisterClassCommandBinding(
|
|
24 typeof(MetroTile),
|
|
25 new CommandBinding(ResetCountCommand, OnResetCountCommand));
|
|
26 }
|
|
27
|
|
28 #endregion
|
|
29
|
|
30 #region Dependency Properties
|
|
31
|
|
32 #region DisplayIcon
|
|
33
|
|
34 /// <summary>
|
|
35 /// Icon for the tile
|
|
36 /// </summary>
|
|
37 public static readonly DependencyProperty DisplayIconProperty =
|
|
38 DependencyProperty.Register("DisplayIcon",
|
|
39 typeof(Image),
|
|
40 typeof(MetroTile),
|
|
41 new PropertyMetadata(null));
|
|
42
|
|
43 public Image DisplayIcon
|
|
44 {
|
|
45 get { return (Image) this.GetValue(DisplayIconProperty); }
|
|
46 set { this.SetValue(DisplayIconProperty, value); }
|
|
47 }
|
|
48 #endregion
|
|
49
|
|
50 #region DisplayCount
|
|
51
|
|
52 /// <summary>
|
|
53 /// Display count for the tile
|
|
54 /// </summary>
|
|
55 public static readonly DependencyProperty DisplayCountProperty =
|
|
56 DependencyProperty.Register("DisplayCount",
|
|
57 typeof(int),
|
|
58 typeof(MetroTile),
|
|
59 new UIPropertyMetadata(0));
|
|
60
|
|
61 public int DisplayCount
|
|
62 {
|
|
63 get { return (int)this.GetValue(DisplayCountProperty); }
|
|
64 set { this.SetValue(DisplayCountProperty, value); }
|
|
65 }
|
|
66
|
|
67 #endregion
|
|
68
|
|
69 #region DisplayText
|
|
70
|
|
71 /// <summary>
|
|
72 /// Main Display text for the tile
|
|
73 /// </summary>
|
|
74 public static readonly DependencyProperty DisplayTextProperty =
|
|
75 DependencyProperty.Register("DisplayText",
|
|
76 typeof(string),
|
|
77 typeof(MetroTile),
|
|
78 new PropertyMetadata("Not set"));
|
|
79
|
|
80 public string DisplayText
|
|
81 {
|
|
82 get { return (string)this.GetValue(DisplayTextProperty); }
|
|
83 set { this.SetValue(DisplayTextProperty, value); }
|
|
84 }
|
|
85 #endregion
|
|
86
|
|
87 #endregion
|
|
88
|
|
89 #region Events
|
|
90
|
|
91 public static readonly RoutedEvent DisplayCountChangedEvent =
|
|
92 EventManager.RegisterRoutedEvent("DisplayCountChanged",
|
|
93 RoutingStrategy.Bubble,
|
|
94 typeof(RoutedEventHandler),
|
|
95 typeof(MetroTile));
|
|
96
|
|
97 public event RoutedEventHandler DisplayCountChanged
|
|
98 {
|
|
99 add { AddHandler(DisplayCountChangedEvent, value); }
|
|
100 remove { RemoveHandler(DisplayCountChangedEvent, value); }
|
|
101 }
|
|
102
|
|
103 protected virtual void OnDisplayCountChanged(int oldValue, int newValue)
|
|
104 {
|
|
105 // 1. Pair of events: A preview that tunnels and a main event that bubbles
|
|
106 // 2. We could also create our own RoutedEventArgs that includes oldValue & new Value
|
|
107
|
|
108 var previewEvent = new RoutedEventArgs(PreviewDisplayCountChangedEvent);
|
|
109 RaiseEvent(previewEvent);
|
|
110
|
|
111 var e = new RoutedEventArgs(DisplayCountChangedEvent);
|
|
112 RaiseEvent(e);
|
|
113 }
|
|
114
|
|
115 public static readonly RoutedEvent PreviewDisplayCountChangedEvent =
|
|
116 EventManager.RegisterRoutedEvent("PreviewDisplayCountChanged",
|
|
117 RoutingStrategy.Tunnel,
|
|
118 typeof(RoutedEventHandler),
|
|
119 typeof(MetroTile));
|
|
120
|
|
121 public event RoutedEventHandler PreviewDisplayCountChanged
|
|
122 {
|
|
123 add { AddHandler(PreviewDisplayCountChangedEvent, value); }
|
|
124 remove { RemoveHandler(PreviewDisplayCountChangedEvent, value); }
|
|
125 }
|
|
126
|
|
127 #endregion
|
|
128
|
|
129 #region Commands
|
|
130
|
|
131 public static readonly ICommand ResetCountCommand =
|
|
132 new RoutedUICommand("ResetCount", "ResetCount", typeof(MetroTile));
|
|
133
|
|
134 private static void OnResetCountCommand(object sender, ExecutedRoutedEventArgs e)
|
|
135 {
|
|
136 var target = (MetroTile)sender;
|
|
137 target.DisplayCount = 0;
|
|
138 }
|
|
139
|
|
140 #endregion
|
|
141
|
|
142 }
|
|
143 } |