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