Silverlight datagrid – doubleclick and enter problems
Posted by admin in .NET, tags: silverlightThese days I started looking into Silverlight and unfortunately my time was spent more on finding sollutions than actually implement something. I am so used to ASP and WinForms that somehow the new way of doing things in SilverLight is making me dizzy.
I started looking into DataGrid mostly and found two strange things:
- There is no double click event on a row
- The <enter> key is not caught in the OnKeyDown handler.
Of course there are solutions to this and based on some nice community posts I made my own implementation for the datagrid which is listed below
public class DataEventArgs : EventArgs
{
private object m_data;
public object Data
{
get { return m_data; }
set { m_data = value; }
}
public DataEventArgs() : base() { }
public DataEventArgs(object data)
: base()
{
this.m_data = data;
}
}
public class CustomGrid : DataGrid
{
private long m_lastTick = 0;
public event EventHandler<DataEventArgs> DoubleClick;
public CustomGrid()
: base()
{
this.MouseLeftButtonUp += new MouseButtonEventHandler(CustomGrid_MouseLeftButtonUp);
}
/// <summary>
/// Handles the MouseLeftButtonUp event of the SilverCommanderGrid control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The instance containing the event data.</param>
void CustomGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if ((DateTime.Now.Ticks - m_lastTick) < 2310000)
{
if (DoubleClick != null)
DoubleClick(this, new DataEventArgs(e.OriginalSource));
}
m_lastTick = DateTime.Now.Ticks;
}
/// <summary>
/// Called before the <see cref="E:System.Windows.UIElement.KeyDown"/> event occurs.
/// Added to suppress the default behaviour on pressing the enter key.
/// </summary>
/// <param name="e">The data for the event.</param>
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key != Key.Enter)
base.OnKeyDown(e);
else
{
e.Handled = true;
this.Focus();
}
}
}
I will come back later with changes to this class if I find any other problems with it. Although maybe there are no changes to be added but only to our attitude towards development. There is a new style of programming brewing on the horizon and we have to weather it somehow.



Entries (RSS)
Interesting – I didn’t knew that there are such limitations in Silverlight – maybe you could use the GetDoubleClickTime (from user32) on Windows to get the max. interval between clicks, but unfortunately you can’t use the same function when Silverlight runs on another OS..
Great post! It gives a solution to my problem! Thanks lot!
Thanks! Seems to have solved my problem “enter”-problem! Couldn’t find a lot of info on this elsewhere.
I am new to silverlight technology
Can you please eleborate with example, how to use this?
how we can use this code on xaml side to get the double click event of datagrid??
You can look at the event argument which is of type DataEventArgs and whose property Data holds he actual OriginalSource property from the MouseButtonEventArgs.This means that if you doubleclick in a cell containing a textblock, the Data property of the event will contain the textblock and you can read the text value from there.
You can also look into the datacontext of the grid to get the current selected element, but that depends on how you did the binding