■ Behavior<T> 클래스를 사용해 마우스 휠을 비활성화하는 방법을 보여준다.
▶ IgnoreMouseWheelBehavior.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 |
using System.Windows.Input; using System.Windows; using Microsoft.Xaml.Behaviors; namespace TestProject { /// <summary> /// 마우스 휠 무사 동작 /// </summary> public sealed class IgnoreMouseWheelBehavior : Behavior<UIElement> { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 부착시 처리하기 - OnAttached() /// <summary> /// 부착시 처리하기 /// </summary> protected override void OnAttached() { base.OnAttached(); AssociatedObject.PreviewMouseWheel += AssociatedObject_PreviewMouseWheel; } #endregion #region 탈착시 처리하기 - OnDetaching() /// <summary> /// 탈착시 처리하기 /// </summary> protected override void OnDetaching() { AssociatedObject.PreviewMouseWheel -= AssociatedObject_PreviewMouseWheel; base.OnDetaching(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 연관 객체 프리뷰 마우스 휠 처리하기 - AssociatedObject_PreviewMouseWheel(sender, e) /// <summary> /// 연관 객체 프리뷰 마우스 휠 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void AssociatedObject_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { e.Handled = true; MouseWheelEventArgs mouseWheelEventArgs = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta); mouseWheelEventArgs.RoutedEvent = UIElement.MouseWheelEvent; AssociatedObject.RaiseEvent(mouseWheelEventArgs); } #endregion } } |
▶ MainWindow.xaml
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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <ListBox Width="200" Height="200"> <ListBoxItem>항목 01</ListBoxItem> <ListBoxItem>항목 02</ListBoxItem> <ListBoxItem>항목 03</ListBoxItem> <ListBoxItem>항목 04</ListBoxItem> <ListBoxItem>항목 05</ListBoxItem> <ListBoxItem>항목 06</ListBoxItem> <ListBoxItem>항목 07</ListBoxItem> <ListBoxItem>항목 08</ListBoxItem> <ListBoxItem>항목 09</ListBoxItem> <ListBoxItem>항목 10</ListBoxItem> <ListBoxItem>항목 11</ListBoxItem> <ListBoxItem>항목 12</ListBoxItem> <ListBoxItem>항목 13</ListBoxItem> <ListBoxItem>항목 14</ListBoxItem> <ListBoxItem>항목 15</ListBoxItem> <ListBoxItem>항목 16</ListBoxItem> <ListBoxItem>항목 17</ListBoxItem> <ListBoxItem>항목 18</ListBoxItem> <ListBoxItem>항목 19</ListBoxItem> <ListBoxItem>항목 20</ListBoxItem> <i:Interaction.Behaviors> <local:IgnoreMouseWheelBehavior /> </i:Interaction.Behaviors> </ListBox> </Window> |