Disable Double Click In WPF Application

Many times, we want to disable the double click of a button in an application. This may be to avoid opening the same popup twice or to avoid saving a new record 2 times.

Whatever the reason, you may need to disable a specific button or, in my case, you may want to do this across the application by default.
 
To do it across the application in a clean way, include the following class in your project.
  1. public static class ControlHelper  
  2. {  
  3.         /// <summary>  
  4.         /// The disable double click property  
  5.         /// </summary>  
  6.         public static readonly DependencyProperty DisableDoubleClickProperty =  
  7.             DependencyProperty.RegisterAttached("DisableDoubleClick"typeof(bool), typeof(ControlHelper), new FrameworkPropertyMetadata(false, OnDisableDoubleClickChanged));  
  8.   
  9.         /// <summary>  
  10.         /// Sets the disable double click.  
  11.         /// </summary>  
  12.         /// <param name="element">The element.</param>  
  13.         /// <param name="value">if set to <c>true</c> [value].</param>  
  14.         public static void SetDisableDoubleClick(UIElement element, bool value)  
  15.         {  
  16.             element.SetValue(DisableDoubleClickProperty, value);  
  17.         }  
  18.   
  19.         /// <summary>  
  20.         /// Gets the disable double click.  
  21.         /// </summary>  
  22.         /// <param name="element">The element.</param>  
  23.         /// <returns></returns>  
  24.         public static bool GetDisableDoubleClick(UIElement element)  
  25.         {  
  26.             return (bool)element.GetValue(DisableDoubleClickProperty);  
  27.         }  
  28.   
  29.         /// <summary>  
  30.         /// Called when [disable double click changed].  
  31.         /// </summary>  
  32.         /// <param name="d">The d.</param>  
  33.         /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>  
  34.         private static void OnDisableDoubleClickChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)  
  35.         {  
  36.             var control = (Control)d;  
  37.             if ((bool)e.NewValue)  
  38.             {  
  39.                 control.PreviewMouseDown += (sender, args) =>  
  40.                 {  
  41.                     if (args.ClickCount > 1)  
  42.                     {  
  43.                         args.Handled = true;  
  44.                     }  
  45.                 };  
  46.             }  
  47.         }  
  48. }  
Assuming, you have a common style defined for buttons in your application to maintain the same look and feel, 
add the following line to the style applied to your buttons in the entire application.
  1. <Setter Property="helpers:ControlHelper.DisableDoubleClick" Value="true"/>   
This should disable the double click of a button by default on all the buttons using that particular style.
 
To apply the same to a specific button, first add a namespace in which the ControlHelper class resides in the XAML page, using the following syntax.
  1. <Window xmlns:helpers="clr-namespace:MentionFulNameSpaceHere">
Then, use the following syntax to declare the button.
  1. <Button helpers:ControlHelper.DisableDoubleClick="true"/>
This should disable double click of a specifc button.