Attach a double click to an object : Event « Windows Presentation Foundation « C# / C Sharp






Attach a double click to an object

        

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AutoCompleteListBuilder
{
    public class GraphicsUtils
    {
        #region Double click

        /// <summary>
        /// Holds the millis for the first click.
        /// </summary>
        private long lastClickedTime = 0;

        /// <summary>
        /// Delegate definition for the call back function.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void DoubleClickHandler(object sender, MouseButtonEventArgs e);

        /// <summary>
        /// Holder for the call back function.
        /// </summary>
        private DoubleClickHandler doubleClickCallBackFunction;
        /// <summary>
        /// This will attach a double click to an object.
        /// </summary>
        /// <remarks>
        /// Due to Silverlight lack of double click event a custom implementation is needed.
        /// this function and its adjacent handlers will attach a double click event to any object
        /// deriving from UIElement.
        /// </remarks>
        /// <param name="doubleClickSender">the UIElement to attach the event to.</param>
        /// <param name="function">the function to call upon the double click event.</param>
        public void AttachDoubleClick(object doubleClickTarget, DoubleClickHandler function)
        {
            UIElement target = (UIElement)doubleClickTarget;
            target.MouseLeftButtonUp += new MouseButtonEventHandler(target_MouseLeftButtonUp);
            target.MouseLeftButtonDown += new MouseButtonEventHandler(target_MouseLeftButtonDown);
            doubleClickCallBackFunction = function;
        }

        private void target_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            lastClickedTime = DateTime.Now.Ticks / 10000;
        }

        private void target_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            long currentMillis = DateTime.Now.Ticks / 10000;

            if (currentMillis - lastClickedTime < 100 && currentMillis - lastClickedTime > 0)
            {
                doubleClickCallBackFunction(sender, e);
            }

        }

        #endregion
    }

}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Cast event sender to a controlCast event sender to a control
2.Press F1 to get helpPress F1 to get help
3.Raise the PropertyChanged eventRaise the PropertyChanged event
4.Get the event sender nameGet the event sender name
5.Use Ellipse event delegateUse Ellipse event delegate
6.Halting event routing with HandledHalting event routing with Handled
7.Use Ellipse.AddHandler to add handler to Ellipse objectsUse Ellipse.AddHandler to add handler to Ellipse objects
8.Get event sender from eventGet event sender from event
9.Handle the ContentRendered eventHandle the ContentRendered event
10.Find source element of an element in event handler by casintFind source element of an element in event handler by casint
11.Add an event handler to an element using codeAdd an event handler to an element using code
12.Handles the Click event on the UniformGridHandles the Click event on the UniformGrid
13.Mark the text control as being changed to prevent any text content or selection changed eventsMark the text control as being changed to prevent any text content or selection changed events
14.Check handler event senderCheck handler event sender
15.Generate Click Events Repeatedly While a Button Is ClickedGenerate Click Events Repeatedly While a Button Is Clicked
16.Raise the ScrollChanged event of a ScrollViewerRaise the ScrollChanged event of a ScrollViewer
17.Event firing sequenceEvent firing sequence
18.ToolBar and event handlerToolBar and event handler
19.Cancel event by setting CanExecute and Handled to falseCancel event by setting CanExecute and Handled to false
20.Activated and Deactivated events monitors the activation status of an application.
21.Event sender, event source and event original sourceEvent sender, event source and event original source