Double-Click Event in Silverlight on FrameworkElement
I was rather astonished at the lack of a double-click event in Silverlight despite its existance in WPF (that I’m aware of). I’ve looked at quite a few solutions online, however they all seemed either complex or felt like more of a “hack”. So, I’m going to show you how gone about solving this by creating a Behavior in Silverlight 4 (this should work for Silverlight 3 as well since that’s when behaviors became available via Expression Blend). This behavior will allow you to add double-click functionality to any FrameworkElement.
Creating the DoubleClickBehavior Class
First off, you’ll need to add a reference to System.Windows.Interactivity which you can find in your Expression Blend installation directory (or as an alternative I’ve also included it in the root directory of the sample project below).
And then you’ll need the code for the behavior:
public class DoubleClickBehavior : Behavior<FrameworkElement> { #region Constants /// <summary> /// The threshold (in miliseconds) between clicks to be considered a double-click. Windows default is 500; I'm a fast clicker. /// </summary> private const int ClickThresholdInMiliseconds = 300; #endregion #region Properties [private] /// <summary> /// Holds the timestamp of the last click. /// </summary> private DateTime? LastClick { get; set; } /// <summary> /// Holds a reference to the instance of the last source object to generate a click. /// </summary> private object LastSource { get; set; } #endregion #region Events /// <summary> /// The event to be raised upon double-click. /// </summary> public event EventHandler<MouseButtonEventArgs> DoubleClick; #endregion #region Behavior Members [overridden] /// <summary> /// This is triggered when the behavior is attached to a FrameworkElement. An event handler is attached to the /// FrameworkElement's MouseLeftButtonUp event. /// </summary> protected override void OnAttached() { base.OnAttached(); this.AssociatedObject.MouseLeftButtonUp += new MouseButtonEventHandler(this.AssociatedObject_MouseLeftButtonUp); } /// <summary> /// This is triggered when the behavior is detached from a FrameworkElement. The event handler attached to the MouseLeftButtonUp /// event is removed. /// </summary> protected override void OnDetaching() { base.OnDetaching(); this.AssociatedObject.MouseLeftButtonUp -= new MouseButtonEventHandler(this.AssociatedObject_MouseLeftButtonUp); } #endregion #region Event Handlers /// <summary> /// Occurs when the MouseLeftButtonDown event is triggered on the object associated to this behavior (this.AssociatedObject). /// </summary> /// <param name="sender">The object which is firing the MouseLeftButtonUp. Note that this is not always the actual source of the event /// since events are bubbled; this is why we access e.OriginalSource</param> /// <param name="e">The MouseButtonEventArgs associated with the MouseLeftButtonDown event.</param> void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (this.LastSource == null || !object.Equals(this.LastSource, e.OriginalSource)) { this.LastSource = e.OriginalSource; this.LastClick = DateTime.Now; } else if ((DateTime.Now - this.LastClick.Value).Milliseconds <= DoubleClickBehavior.ClickThresholdInMiliseconds) { this.LastClick = null; this.LastSource = null; if (this.DoubleClick != null) this.DoubleClick(sender, e); } else { this.LastClick = null; this.LastSource = null; } } #endregion }
Simple enough; the comments should be enough to explain the code, as the code is very basic.
Implementing the DoubleClickBehavior Behavior
Now, how to actually implement the behavior in XAML. First things first, we need to add the following clr-namespaces to our page:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:behaviors="clr-namespace:Double_Click_Behavior.Behaviors"
Once we have done that, we can associate the behavior with a FrameworkElement. In this example, I’m associating our double-click behavior with a TextBlock control:
<TextBlock Text="Double Click Me!"> <i:Interaction.Behaviors> <behaviors:DoubleClickBehavior DoubleClick="DoubleClickBehavior_DoubleClick" /> </i:Interaction.Behaviors> </TextBlock>
Now, whenever the TextBlock is double-clicked it will raise the DoubleClickBehavior_DoubleClick method on our page. This can of course be adapted to MVVM if you want to expand the DoubleClickBehavior to use commanding, however I will not go into that in this post.
And that’s it! Download the sample project (VS2010) for a working Silverlight 4 example. Enjoy!
I have included a copy of System.Windows.Interactivity in the root directory of this solution.
It’s not a behavior but this solution might be simpler and faster if you just want something quick and dirty.
http://csharping.com/silverlight/a-simple-double-click-handler-for-silverlight-wpf/
Thanks, I have hunted high and low for a solution for this problem and yours is the first that I have found to work.