System.Windows.Controls.InteractionHelper.AllowMouseLeftButtonDown C# (CSharp) Method

AllowMouseLeftButtonDown() public method

Check if the control's MouseLeftButtonDown event should be handled.
public AllowMouseLeftButtonDown ( MouseButtonEventArgs e ) : bool
e MouseButtonEventArgs Event arguments.
return bool
        public bool AllowMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if(e == null)
            {
                throw new ArgumentNullException("e");
            }

            bool enabled = Control.IsEnabled;
            if(enabled)
            {
                // Get the current position and time
                DateTime now = DateTime.UtcNow;
                Point position = e.GetPosition(Control);

                // Compute the deltas from the last click
                double timeDelta = (now - LastClickTime).TotalMilliseconds;
                Point lastPosition = LastClickPosition;
                double dx = position.X - lastPosition.X;
                double dy = position.Y - lastPosition.Y;
                double distance = dx * dx + dy * dy;

                // Check if the values fall under the sequential click temporal
                // and spatial thresholds
                if(timeDelta < SequentialClickThresholdInMilliseconds &&
                    distance < SequentialClickThresholdInPixelsSquared)
                {
                    // TODO: Does each click have to be within the single time
                    // threshold on WPF?
                    ClickCount++;
                }
                else
                {
                    ClickCount = 1;
                }

                // Set the new position and time
                LastClickTime = now;
                LastClickPosition = position;

                // Raise the event
                IsPressed = true;
            }
            else
            {
                ClickCount = 1;
            }

            return enabled;
        }

Usage Example

 /// <summary>
 /// Provides handling for the MouseLeftButtonDown event.
 /// </summary>
 /// <param name="e">The data for the event.</param>
 protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
 {
     if (_interaction.AllowMouseLeftButtonDown(e))
     {
         _interaction.OnMouseLeftButtonDownBase();
         base.OnMouseLeftButtonDown(e);
     }
 }