I've been finding various methods of dealing with double click and then the authors slap on some if code for handling single clicks. Is there a standard now in Silverlight 3 that everyone is using to handle both a single and a double click on listboxes?
A:
Here's a class I've implemented for controls and also a second derived class below for a treeview (Silverlight Toolkit). Just instantiate it with the control you want to check for double clicks and add a handler for the DoubleClicked event. It uses a timer to try to simulate a double-click event. You can change the delay if you think it will work better.
Public Class DoubleClickHelper
Public Event DoubleClicked(ByVal sender As FrameworkElement)
Private WithEvents UI As FrameworkElement
Sub New(ByRef UI As FrameworkElement)
Me.UI = UI
UI.AddHandler(UIElement.MouseLeftButtonDownEvent, New MouseButtonEventHandler(AddressOf UI_MouseLeftButtonDown), True)
InitTimer()
End Sub
Public Delay As Single = 0.2
Private _dblclick As Boolean = False
Private _timer As New System.Windows.Threading.DispatcherTimer()
Protected Property DoubleClick() As Boolean
Get
Return _dblclick
End Get
Set(ByVal value As Boolean)
_dblclick = value
InitTimer()
End Set
End Property
Private Sub InitTimer()
RemoveHandler _timer.Tick, AddressOf timer_Tick
_timer.Stop()
_timer = New System.Windows.Threading.DispatcherTimer()
_timer.Interval = TimeSpan.FromSeconds(Delay)
AddHandler _timer.Tick, AddressOf timer_Tick
_timer.Start()
End Sub
Protected Overridable Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
DoubleClick = False
End Sub
Protected Overridable Sub UI_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles UI.MouseLeftButtonDown
If DoubleClick Then
HandleDoubleClick(sender)
Else
HandleFirstClick(sender)
End If
End Sub
Protected Overridable Sub HandleDoubleClick(ByVal sender As FrameworkElement)
RaiseEvent DoubleClicked(sender)
End Sub
Protected Overridable Sub HandleFirstClick(ByVal sender As FrameworkElement)
DoubleClick = True
End Sub
End Class
Public Class TreeViewItemDoubleClickHelper
Inherits DoubleClickHelper
Private SameSelection As Boolean = False
Private WithEvents TreeView As TreeView = Nothing
Public Sub New(ByVal TreeView As TreeView)
MyBase.New(TreeView)
Me.TreeView = TreeView
End Sub
'This event happens after MouseLeftButtonDown
Private Sub TreeView_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles TreeView.SelectedItemChanged
SameSelection = e.OldValue Is e.NewValue
End Sub
Protected Overrides Sub UI_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
'MyBase.UI_MouseLeftButtonDown(sender, e)
If DoubleClick Or SameSelection Then
HandleDoubleClick(sender)
SameSelection = False
DoubleClick = False
Else
HandleFirstClick(sender)
End If
End Sub
End Class
Paully
2009-08-20 06:51:59
+3
A:
If you use the Reactive Extensions (Rx) library the code to support double click is much simpler:
Observable.FromEvent<MouseButtonEventArgs>(myControl, "MouseLeftButtonDown").TimeInterval().Subscribe(evt =>
{
if (evt.Interval.Milliseconds <= 300)
{
// Do something on double click
}
});
Flatliner DOA
2010-01-21 03:09:27
A:
I haven't tried it but this looks promising: http://compiledexperience.com/blog/posts/Silverlight-3-Behaviors-Double-Click-Trigger
Mark
2010-01-22 00:50:59