Simulate a double-click event in versions earlier than Silverlight 4

Source: Internet
Author: User
Tags event timer

The new feature of Silverlight 5 includes the double-click event. See the following code.

<Grid x: Name = "LayoutRoot" Background = "White">
<Ellipse Height = "103" HorizontalAlignment = "Left" Fill = "Green" Margin = "117,56"
Name = "ellipse1" Stroke = "Black" StrokeThickness = "1" verticalignment = "Top"
Width = "158" MouseLeftButtonDown = "ellipse1_MouseLeftButtonDown"
MouseRightButtonDown = "ellipse2_MouseRightButtonDown"/>
</Grid>

In event implementation:

Private void ellipse1_MouseLeftButtonDown (object sender, MouseButtonEventArgs e)
{
// Determine whether the mouse is clicked twice within the double-click interval set by the system. The window is displayed.
If (e. ClickCount = 2)
{
MessageBox. Show ("left mouse click" + e. ClickCount. ToString ());
}
}

 

However, versions earlier than Silverlight4 do not contain the ClickCount attribute, so they can only be implemented by themselves. Therefore, a function that simulates double-clicking the mouse is implemented:

Public class MouseTool
{
// Double-click the Event timer
Private DispatcherTimer _ timer;
// Whether to click once
Private bool _ isFirst;

Public MouseTool ()
{
This. _ timer = new DispatcherTimer ();
This. _ timer. Interval = new TimeSpan (0, 0, 0, 0,400 );
This. _ timer. Tick + = new EventHandler (this. _ timer_Tick );
}

/// <Summary>
/// Determine whether to double-click
/// </Summary>
/// <Returns> </returns>
Public bool IsDoubleClick ()
{
If (! This. _ isFirst)
{
This. _ isFirst = true;
This. _ timer. Start ();
Return false;
}
Else
Return true;
}

// Interval
Void _ timer_Tick (object sender, EventArgs e)
{
This. _ isFirst = false;
This. _ timer. Stop ();
}
}

The event call is as follows:

Private MouseTool _ mouseTool = new MouseTool ();

Public void GridSplitter_MouseLeftButtonDown (object sender, MouseButtonEventArgs e)
{
If (this. _ mouseTool. IsDoubleClick ())
{
//...

That's easy!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.