-
Notifications
You must be signed in to change notification settings - Fork 38
/
MouseOver.cs
89 lines (81 loc) · 3.3 KB
/
MouseOver.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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace SapphireBootWPF
{
/// <summary>
/// An attached property provider for setting the content of a control
/// when the mouse is over top.
/// </summary>
public class MouseOver
{
/// <summary>
/// Specifies the ImageSource when the mouse is over the control.
/// </summary>
public static readonly DependencyProperty HoverImageSourceProperty =
DependencyProperty.RegisterAttached(
"HoverImageSource", typeof(ImageSource), typeof(MouseOver),
new PropertyMetadata(HoverImageSourcePropertyChanged));
public static ImageSource GetHoverImageSource(Image target)
{
return (ImageSource)target.GetValue(HoverImageSourceProperty);
}
public static void SetHoverImageSource(Image target, ImageSource value)
{
target.SetValue(HoverImageSourceProperty, value);
}
private static void HoverImageSourcePropertyChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Image target = d as Image;
// Remove existing handlers first.
RemoveHandlers(target);
// Register handlers with the target.
System.Diagnostics.Debug.WriteLine(
"Registering event mouse handlers for " + target.Name,
"ImageSourcePropertyChanged");
target.MouseEnter += target_MouseEnter;
target.MouseLeave += target_MouseLeave;
target.Unloaded += target_Unloaded;
System.Diagnostics.Debug.WriteLine(
"Registered event mouse handlers for " + target.Name,
"ImageSourcePropertyChanged");
}
private static void RemoveHandlers(Image target)
{
System.Diagnostics.Debug.WriteLine(
"Removing event mouse handlers for " + target.Name,
"RemoveHandlers");
target.MouseEnter -= target_MouseEnter;
target.MouseLeave -= target_MouseLeave;
target.Unloaded -= target_Unloaded;
System.Diagnostics.Debug.WriteLine(
"Removed event mouse handlers for " + target.Name,
"RemoveHandlers");
}
static void target_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
// Restore the original ImageSource.
Image target = sender as Image;
target.Source = (ImageSource)target.Tag;
System.Diagnostics.Debug.WriteLine(
"Restored image source with original image...",
"target_MouseLeave");
}
static void target_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
// Save the original ImageSource first.
Image target = sender as Image;
target.Tag = target.Source;
target.Source = GetHoverImageSource(target);
System.Diagnostics.Debug.WriteLine(
"Updated image source with MouseOver image...",
"target_MouseEnter");
}
static void target_Unloaded(object sender, RoutedEventArgs e)
{
RemoveHandlers(sender as Image);
}
}
}