Advertisement
andrew4582

MouseDownBehavior WPF

Apr 14th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7.  
  8. namespace UI.Behaviors
  9. {
  10.     /// <summary>
  11.     /// Behavoir to handle elements Mouse Down Event
  12.     /// <remarks>
  13.     /// bhv:MouseDownBehavior.Command="{Binding MouseDownCommand}"
  14.     /// bhv:MouseDownBehavior.UsePreviewMouseDown="True"
  15.     /// </remarks>
  16.     /// </summary>
  17.     public static class MouseDownBehavior
  18.     {
  19.         #region Command Dependency Property
  20.  
  21.         public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
  22.             "Command",
  23.             typeof(ICommand),
  24.             typeof(MouseDownBehavior),
  25.             new UIPropertyMetadata(null, OnCommandChanged));
  26.  
  27.  
  28.         public static ICommand GetCommand(DependencyObject obj)
  29.         {
  30.             return (ICommand)obj.GetValue(CommandProperty);
  31.         }
  32.  
  33.         public static void SetCommand(DependencyObject obj, ICommand value)
  34.         {
  35.             obj.SetValue(CommandProperty, value);
  36.         }
  37.         #endregion
  38.  
  39.         #region UsePreviewMouseDown Dependency Property
  40.  
  41.         public static readonly DependencyProperty UsePreviewMouseDownProperty = DependencyProperty.RegisterAttached(
  42.             "UsePreviewMouseDown",
  43.             typeof(bool),
  44.             typeof(MouseDownBehavior),
  45.             new UIPropertyMetadata(false));
  46.  
  47.  
  48.         public static bool GetUsePreviewMouseDown(DependencyObject obj)
  49.         {
  50.             return (bool)obj.GetValue(UsePreviewMouseDownProperty);
  51.         }
  52.  
  53.         public static void SetUsePreviewMouseDown(DependencyObject obj, bool value)
  54.         {
  55.             obj.SetValue(UsePreviewMouseDownProperty, value);
  56.         }
  57.         #endregion
  58.  
  59.         static void OnCommandChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
  60.         {
  61.             var ele = sender as UIElement;
  62.             if (ele == null)
  63.                 return;
  64.  
  65.             if (args.OldValue != null)
  66.             {
  67.                 if (GetUsePreviewMouseDown(ele))
  68.                     ele.PreviewMouseDown -= OnMouseDown;
  69.                 else
  70.                     ele.MouseDown -= OnMouseDown;
  71.             }
  72.  
  73.             if (args.NewValue != null)
  74.             {
  75.                 if (GetUsePreviewMouseDown(ele))
  76.                     ele.PreviewMouseDown += OnMouseDown;
  77.                 else
  78.                     ele.MouseDown += OnMouseDown;
  79.             }
  80.         }
  81.  
  82.         static void OnMouseDown(object sender, MouseButtonEventArgs e)
  83.         {
  84.             var ele = sender as UIElement;
  85.             if (ele == null)
  86.                 return;
  87.  
  88.             var command = GetCommand(ele);
  89.             if (command != null)
  90.             {
  91.                 if (command.CanExecute(e))
  92.                 {
  93.                     command.Execute(e);
  94.                 }
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement