Following is the Code snippet for the List Box Item double click event using Silverlight 4:
XAML (MainPage.Xaml):
CODE(MainPage.Xaml.cs):
SOURCE DOWNLOAD LINK:
ListBox Double Click
XAML (MainPage.Xaml):
using System;SAMPLE OUTPUT:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Browser;
using System.Windows.Input;
namespace ListBoxDoubleClick
{
public partial class MainPage : UserControl {
Point _clickPosition;
public DateTime _lastClick = DateTime.Now;
private bool _firstClickDone = false;
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 1; i < 11; i++)
{
ListBoxItem lbitem = new ListBoxItem();
lbitem.Name = "lb_" + i;
lbitem.Content = "This is ListBoxItem No:" + i;
lbitem.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(ListBoxItem_DoubleClickEvent), true);
listBox1.Items.Add(lbitem);
}
}
private void ListBoxItem_DoubleClickEvent(object sender, MouseButtonEventArgs e)
{
ListBoxItem element = sender as ListBoxItem;
if (element == null)
return;
DateTime clickTime = DateTime.Now;
TimeSpan span = clickTime - _lastClick;
if (span.TotalMilliseconds > 300 || _firstClickDone == false)
{
//Single Click lblMsg.Content = string.Format("ListBoxItem \" {0}\" is Single clicked. ", element.Content );
_clickPosition = e.GetPosition(element);
_firstClickDone = true;
_lastClick = DateTime.Now;
}
else {
Point position = e.GetPosition(element);
//Double Click if (Math.Abs(_clickPosition.X - position.X) < 4 && Math.Abs(_clickPosition.Y - position.Y) < 4) //mouse didn't move => Valid double click {
lblMsg.Content = string.Format("ListBoxItem \" {0}\" is Double clicked. ", element.Content);
/// Your logic goes here for double click event /// }
_firstClickDone = false;
}
}
}
}
SOURCE DOWNLOAD LINK:
ListBox Double Click
No comments:
Post a Comment