Edit cell on single clicl only, and not double click ?

2 posts, 0 answers
  1. Michael
    Michael avatar
    2 posts
    Member since:
    Feb 2014

    Posted 23 Jul 2014 Link to this post

    Hi.

    I was wondering if it's possible with a RadGridView to have the double click event not enable edits on the cell ?
    Single click edits work fine, but double clicks activate another view, and I do not want the cell being in an editable state at this point.

    Is there a solution to this ?

    Thanks.
  2. Boris
    Admin
    Boris avatar
    276 posts

    Posted 25 Jul 2014 Link to this post

    Hello Michael,

    A possible approach is to use the BeginningEdit event to cancel the edit of the cell, when the double click occurs. In order to do that you can do the following:

    1)  set two Boolean flags(isEditMouseInitiated and isDoubleClick). Where isEditMouseInitiated and isDoubleClick are used to indicate, respectively that an edit operation was initiated by a mouse activity and you have a double click.
    2)  add a handlers for the MouseDoubleClickEvent and MouseLeftButtonUpEvent events, to set the Boolean flags to the appropriate values - isDoubleClick is set to false, when the MouseDoubleClickEvent event triggers and the isEditMouseInitiated is set to true, when the MouseLeftButtonUpEvent triggers.

    bool isDoubleClick = true;
    bool isEditMouseInitiated = true;
     
    DispatcherTimer dispatcherTimer = new DispatcherTimer();
    public MainWindow()
    {
        InitializeComponent();
     
        this.AddHandler(Control.MouseDoubleClickEvent, new MouseButtonEventHandler((s, e) =>
        {
            isDoubleClick = false;
        }), true);
        this.AddHandler(Control.MouseLeftButtonUpEvent, new MouseButtonEventHandler((s, e) =>
        {
            isEditMouseInitiated = true;
        }), true);
    }

    3)  use a DispatcherTimer in the BeginingEdit event handler to set an interval of time and attach an event handler to it's Tick event to detect if a second click has occurred. If the isDoubleClick flag or the IsEnabled property of the DispatcherTimer are set to false the GridView will not enter edit mode.


     
            private void clubsGrid_BeginningEdit(object sender, GridViewBeginningEditRoutedEventArgs e)
            {
                if (isEditMouseInitiated)
                {
                    e.Cancel = true;
                    dispatcherTimer.Tick += dispatcherTimer_Tick;
                    dispatcherTimer.Interval = new TimeSpan(2500000);
                    dispatcherTimer.Start();
                }
            }
     
            void dispatcherTimer_Tick(object sender, EventArgs e)
            {
                if (dispatcherTimer.IsEnabled && isDoubleClick)
                {
                    clubsGrid.BeginEdit();
                    isEditMouseInitiated = false;
                }
                else
                {
                    isDoubleClick = true;
                }
     
                dispatcherTimer.Stop();
            }

    Also, I attached a sample project that demonstrates the suggested approach.

    I hope this helps.

    Regards,
    Boris Penev
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top