-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathHotTrack.xaml.cs
319 lines (287 loc) · 12.7 KB
/
HotTrack.xaml.cs
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
namespace BmLib.Controls
{
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
/// <summary>
/// HotTrack is a re-styled Border that highlight itself when IsMouseOver,
/// IsDragging or IsSelected
/// </summary>
[TemplatePart(Name = "PART_HotTrackGrid", Type = typeof(Grid))]
[TemplatePart(Name = "PART_Selected", Type = typeof(Rectangle))]
[TemplatePart(Name = "PART_Background", Type = typeof(Rectangle))]
[TemplatePart(Name = "PART_Highlight", Type = typeof(Rectangle))]
[TemplateVisualState(Name = "State_Normal", GroupName = "CommonStates")]
[TemplateVisualState(Name = "State_MouseOver", GroupName = "CommonStates")]
[TemplateVisualState(Name = "State_MouseOverGrayed", GroupName = "CommonStates")]
[TemplateVisualState(Name = "State_Dragging", GroupName = "CommonStates")]
[TemplateVisualState(Name = "State_Selected", GroupName = "CommonStates")]
////[TemplateVisualState(Name = "Focused", GroupName = "FocusedStates")]
////[TemplateVisualState(Name = "Unfocused", GroupName = "FocusedStates")]
public class HotTrack : ContentControl
{
#region fields
/// <summary>
/// Implements the backing store of the <see cref="IsSelected"/> dependency property.
/// </summary>
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register("IsSelected", typeof(bool),
typeof(HotTrack), new UIPropertyMetadata(false,
new PropertyChangedCallback(OnIsSelectedChanged)));
/// <summary>
/// Implements the backing store of the <see cref="SelectedBorderBrush"/> dependency property.
/// </summary>
public static readonly DependencyProperty SelectedBorderBrushProperty =
DependencyProperty.Register("SelectedBorderBrush", typeof(Brush),
typeof(HotTrack), new UIPropertyMetadata(Brushes.Transparent));
/// <summary>
/// Implements the backing store of the <see cref="BackgroundBrush"/> dependency property.
/// </summary>
public static readonly DependencyProperty BackgroundBrushProperty =
DependencyProperty.Register("BackgroundBrush", typeof(Brush),
typeof(HotTrack), new UIPropertyMetadata(SystemColors.HotTrackBrush));
/// <summary>
/// Implements the backing store of the <see cref="SelectedBrush"/> dependency property.
/// </summary>
public static readonly DependencyProperty SelectedBrushProperty =
DependencyProperty.Register("SelectedBrush", typeof(Brush),
typeof(HotTrack), new UIPropertyMetadata(SystemColors.ActiveCaptionBrush));
/// <summary>
/// Implements the backing store of the <see cref="HighlightBrush"/> dependency property.
/// </summary>
public static readonly DependencyProperty HighlightBrushProperty =
DependencyProperty.Register("HighlightBrush", typeof(Brush),
typeof(HotTrack), new UIPropertyMetadata(new SolidColorBrush(Color.FromArgb(117, 255, 255, 255))));
/// <summary>
/// Implements the backing store of the <see cref="CornerRadius"/> dependency property.
/// </summary>
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register("CornerRadius", typeof(CornerRadius),
typeof(HotTrack), new UIPropertyMetadata(new CornerRadius(0)));
/// <summary>
/// Implements the backing store of the <see cref="IsDragging"/> dependency property.
/// </summary>
public static readonly DependencyProperty IsDraggingProperty =
DependencyProperty.Register("IsDragging", typeof(bool),
typeof(HotTrack), new UIPropertyMetadata(false, OnIsSelectedChanged));
/// <summary>
/// Implements the backing store of the <see cref="IsDraggingOver"/> dependency property.
/// </summary>
public static readonly DependencyProperty IsDraggingOverProperty =
DependencyProperty.Register("IsDraggingOver", typeof(bool),
typeof(HotTrack), new UIPropertyMetadata(false, OnIsSelectedChanged));
/// <summary>
/// Implements the backing store of the <see cref="FillFullRow"/> dependency property.
/// </summary>
public static readonly DependencyProperty FillFullRowProperty =
DependencyProperty.Register("FillFullRow", typeof(bool),
typeof(HotTrack), new UIPropertyMetadata(false));
/// <summary>
/// Implements the backing store of the <see cref="UseTransition"/> dependency property.
/// </summary>
public static readonly DependencyProperty UseTransitionProperty =
DependencyProperty.Register("UseTransition", typeof(bool), typeof(HotTrack), new PropertyMetadata(true));
/// <summary>
/// Implements the backing store of the <see cref="ThreeDStyle"/> dependency property.
/// </summary>
public static readonly DependencyProperty ThreeDStyleProperty =
DependencyProperty.Register("ThreeDStyle",
typeof(bool),
typeof(HotTrack),
new PropertyMetadata(false));
#endregion fields
#region Constructor
/// <summary>
/// Static class constructor.
/// </summary>
static HotTrack()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HotTrack),
new FrameworkPropertyMetadata(typeof(HotTrack)));
}
#endregion
#region properties
/// <summary>
/// Gets/sets whether this item is selected or not.
/// </summary>
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { this.SetValue(IsSelectedProperty, value); }
}
/// <summary>
/// Gets/sets the <see cref="Brush"/> that is applied on the border of this item
/// when it is selected.
/// </summary>
public Brush SelectedBorderBrush
{
get { return (Brush)GetValue(SelectedBorderBrushProperty); }
set { this.SetValue(SelectedBorderBrushProperty, value); }
}
/// <summary>
/// Gets/sets the <see cref="Brush"/> that is applied on the background of this item.
/// </summary>
public Brush BackgroundBrush
{
get { return (Brush)GetValue(BackgroundBrushProperty); }
set { this.SetValue(BackgroundBrushProperty, value); }
}
/// <summary>
/// Gets/sets the <see cref="Brush"/> that is applied when the item is selected.
/// </summary>
public Brush SelectedBrush
{
get { return (Brush)GetValue(SelectedBrushProperty); }
set { this.SetValue(SelectedBrushProperty, value); }
}
/// <summary>
/// Gets/sets the <see cref="Brush"/> that is applied when the item is highlighted.
/// </summary>
public Brush HighlightBrush
{
get { return (Brush)GetValue(HighlightBrushProperty); }
set { this.SetValue(HighlightBrushProperty, value); }
}
/// <summary>
/// Gets/sets the corner radius of this controls boder.
/// </summary>
public CornerRadius CornerRadius
{
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
set { this.SetValue(CornerRadiusProperty, value); }
}
/// <summary>
/// Gets/sets wether the control is currently being dragged or not.
/// </summary>
public bool IsDragging
{
get { return (bool)GetValue(IsDraggingProperty); }
set { this.SetValue(IsDraggingProperty, value); }
}
/// <summary>
/// Gets/sets wether the control is currently being dragged over or not.
/// </summary>
public bool IsDraggingOver
{
get { return (bool)GetValue(IsDraggingOverProperty); }
set { this.SetValue(IsDraggingOverProperty, value); }
}
/// <summary>
/// For TreeView, create a mirror to completely highlight the whole row.
/// </summary>
public bool FillFullRow
{
get { return (bool)GetValue(FillFullRowProperty); }
set { this.SetValue(FillFullRowProperty, value); }
}
/// <summary>
/// Gets/sets whether or not to use transitions in animations (???)
/// </summary>
public bool UseTransition
{
get { return (bool)GetValue(UseTransitionProperty); }
set { this.SetValue(UseTransitionProperty, value); }
}
/// <summary>
/// Gets/sets whether Hotrack should be styled with 3D emulated
/// surface impression or not.
/// </summary>
public bool ThreeDStyle
{
get { return (bool)GetValue(ThreeDStyleProperty); }
set { this.SetValue(ThreeDStyleProperty, value); }
}
#endregion properties
#region Methods
/// <summary>
/// Is called when a control template is applied.
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.UpdateStates(false);
}
/// <summary>
/// Invoked when an unhandled System.Windows.Input.Mouse.MouseEnter attached event
/// is raised on this element. Implement this method to add class handling for this
/// event.
/// </summary>
/// <param name="e">The System.Windows.Input.MouseEventArgs that contains the event data.</param>
protected override void OnMouseEnter(MouseEventArgs e)
{
base.OnMouseEnter(e);
this.UpdateStates(this.UseTransition);
}
/// <summary>
/// Invoked when an unhandled System.Windows.Input.Mouse.MouseLeave attached event
/// is raised on this element. Implement this method to add class handling for this
/// event.
/// </summary>
/// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/>
/// that contains the event data.</param>
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
this.UpdateStates(this.UseTransition);
}
private static void OnIsSelectedChanged(object sender, DependencyPropertyChangedEventArgs args)
{
HotTrack ht = (HotTrack)sender;
if (!(args.NewValue.Equals(args.OldValue)))
{
ht.UpdateStates(ht.UseTransition);
}
}
/// <summary>
/// Is executed when the corresponding visual state of the control is changing.
/// Eg: If Mouse is over control, Mouse is leaving control, is enabled, is disabled, etc
/// </summary>
/// <param name="useTransition"></param>
private void UpdateStates(bool useTransition)
{
if (this.IsSelected)
{
VisualStateManager.GoToState(this, "State_Selected", useTransition);
}
else
{
if (this.IsDragging)
{
VisualStateManager.GoToState(this, "State_Dragging", useTransition);
}
else
{
if (this.IsDraggingOver)
{
VisualStateManager.GoToState(this, "State_DraggingOver", useTransition);
}
else
{
if (this.IsMouseOver)
{
if (this.IsEnabled)
{
VisualStateManager.GoToState(this, "State_MouseOver", useTransition);
}
else
{
VisualStateManager.GoToState(this, "State_MouseOverGrayed", useTransition);
}
}
else
{
VisualStateManager.GoToState(this, "State_Normal", useTransition);
}
}
}
}
////if (IsFocused)
//// VisualStateManager.GoToState(this, "Focused", useTransition);
////else
//// VisualStateManager.GoToState(this, "Unfocused", useTransition);
}
#endregion
}
}