■ Behavior<T> 클래스에서 투명도 Behavior를 구현하는 방법을 보여준다.
▶ Expression Blend 참조 추가 (XAML)
1 2 3 |
Microsoft.Expression.Interactions |
▶ 예제 코드 (C#)
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 |
using System; using System.Windows; using System.Windows.Input; using System.Windows.Interactivity; namespace TestProject { /// <summary> /// 투명도 비헤이비어 /// </summary> public class OpacityBehavior : Behavior<UIElement> { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected //////////////////////////////////////////////////////////////////////////////// Function #region 부착시 처리하기 - OnAttached() /// <summary> /// 부착시 처리하기 /// </summary> protected override void OnAttached() { base.OnAttached(); AssociatedObject.MouseWheel += new MouseWheelEventHandler(AssociatedObject_MouseWheel); } #endregion #region 탈착중 처리하기 - OnDetaching() /// <summary> /// 탈착중 처리하기 /// </summary> protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.MouseWheel -= new MouseWheelEventHandler(AssociatedObject_MouseWheel); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 결합 객체 마우스 휠시 처리하기 - AssociatedObject_MouseWheel(sender, e) /// <summary> /// 결합 객체 마우스 휠시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void AssociatedObject_MouseWheel(object sender, MouseWheelEventArgs e) { if(e.Delta > 0) { AssociatedObject.Opacity = Math.Max(0d, AssociatedObject.Opacity - 0.1d); } else if(e.Delta < 0) { AssociatedObject.Opacity = Math.Min(1d, AssociatedObject.Opacity + 0.1d); } } #endregion } } |