These 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:

  1. There is no double click event on a row
  2. 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(thisnew 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.

4 Responses to “Silverlight datagrid – doubleclick and enter problems”
  1. Tudor says:

    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..

  2. Siva says:

    Great post! It gives a solution to my problem! Thanks lot!

  3. Fredrik says:

    Thanks! Seems to have solved my problem “enter”-problem! Couldn’t find a lot of info on this elsewhere.

  4. Jimmy says:

    I am new to silverlight technology

    Can you please eleborate with example, how to use this?

  5.  
Leave a Reply