markus / KCOM / Common / ExpandingMouseOver.cs @ 65768148
이력 | 보기 | 이력해설 | 다운로드 (3.49 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Windows; |
6 |
using System.Windows.Input; |
7 |
using System.Windows.Interactivity; |
8 |
using System.Windows.Media; |
9 |
using System.Windows.Media.Animation; |
10 |
|
11 |
namespace KCOM.Common |
12 |
{ |
13 |
public class ExpandingMouseOver : Behavior<UIElement> |
14 |
{ |
15 |
private Storyboard _storyboard; |
16 |
private DoubleAnimation _daScaleX, _daScaleY; |
17 |
private ScaleTransform _scScale; |
18 |
|
19 |
public static readonly DependencyProperty ScaleFactorProperty = DependencyProperty.Register("ScaleFactor", typeof(double), typeof(ExpandingMouseOver), new PropertyMetadata(1.5)); |
20 |
|
21 |
public double ScaleFactor |
22 |
{ |
23 |
get |
24 |
{ |
25 |
return (double)GetValue(ScaleFactorProperty); |
26 |
} |
27 |
set |
28 |
{ |
29 |
SetValue(ScaleFactorProperty, value); |
30 |
} |
31 |
} |
32 |
|
33 |
protected override void OnAttached() |
34 |
{ |
35 |
base.OnAttached(); |
36 |
|
37 |
AssociatedObject.MouseEnter += new MouseEventHandler(OnMouseEnter); |
38 |
AssociatedObject.MouseLeave += new MouseEventHandler(OnMouseLeave); |
39 |
AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(OnLeftButtonDown); |
40 |
|
41 |
_scScale = new ScaleTransform(); |
42 |
AssociatedObject.RenderTransformOrigin = new Point(0.5, 0.5); |
43 |
|
44 |
|
45 |
|
46 |
if (AssociatedObject.RenderTransform == null) |
47 |
{ |
48 |
AssociatedObject.RenderTransform = _scScale; |
49 |
} |
50 |
else |
51 |
{ |
52 |
if (AssociatedObject.RenderTransform is TransformGroup) |
53 |
{ |
54 |
((TransformGroup)AssociatedObject.RenderTransform).Children.Add(_scScale); |
55 |
} |
56 |
else |
57 |
{ |
58 |
TransformGroup group = new TransformGroup(); |
59 |
group.Children.Add(AssociatedObject.RenderTransform); |
60 |
group.Children.Add(_scScale); |
61 |
AssociatedObject.RenderTransform = group; |
62 |
} |
63 |
} |
64 |
|
65 |
|
66 |
|
67 |
_daScaleX = new DoubleAnimation(); |
68 |
_daScaleY = new DoubleAnimation(); |
69 |
_daScaleX.Duration = _daScaleY.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 150)); |
70 |
|
71 |
Storyboard.SetTarget(_daScaleX, _scScale); |
72 |
Storyboard.SetTarget(_daScaleY, _scScale); |
73 |
Storyboard.SetTargetProperty(_daScaleX, new PropertyPath("ScaleX")); |
74 |
Storyboard.SetTargetProperty(_daScaleY, new PropertyPath("ScaleY")); |
75 |
|
76 |
_storyboard = new Storyboard(); |
77 |
_storyboard.Children.Add(_daScaleX); |
78 |
_storyboard.Children.Add(_daScaleY); |
79 |
|
80 |
} |
81 |
|
82 |
|
83 |
|
84 |
protected override void OnDetaching() |
85 |
{ |
86 |
base.OnDetaching(); |
87 |
|
88 |
AssociatedObject.MouseEnter -= new MouseEventHandler(OnMouseEnter); |
89 |
AssociatedObject.MouseLeave -= new MouseEventHandler(OnMouseLeave); |
90 |
AssociatedObject.MouseLeftButtonDown -= new MouseButtonEventHandler(OnLeftButtonDown); |
91 |
} |
92 |
|
93 |
void OnMouseEnter(object sender, MouseEventArgs e) |
94 |
{ |
95 |
_daScaleX.To = _daScaleY.To = ScaleFactor; |
96 |
_storyboard.Begin(); |
97 |
} |
98 |
|
99 |
void OnMouseLeave(object sender, MouseEventArgs e) |
100 |
{ |
101 |
_daScaleX.To = _daScaleY.To = 1.0; |
102 |
_storyboard.Begin(); |
103 |
} |
104 |
void OnLeftButtonDown(object sender, MouseButtonEventArgs e) |
105 |
{ |
106 |
_daScaleX.To = _daScaleY.To = 1.0; |
107 |
_storyboard.Begin(); |
108 |
} |
109 |
} |
110 |
} |