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!