Commit edit when clicking on area outside of the GridView

8 posts, 2 answers
  1. BENN
    BENN avatar
    59 posts
    Member since:
    Dec 2011

    Posted 20 Jun Link to this post

    Well, it used to work, at least on version 2012.3 (I've verified it)

    However, if you do the same on 2016.2, then it doesn't work. The cell is left in some kind of edit mode, while the keyboard cursor is not blinking.

     

    This is frustrating, since after updating to the new version, I'm wasting more time on fixing stuff that used to work than writing new features, and I'm sure that I'll find more issues in the future.

     

    Is there something that can be configured in order to make it work like it used to? I already tried ActionOnLostFocus="CommitEdit"

    Why was this feature removed? It seems only logical to exit edit mode when clicking anywhere outside the edited cell.

     

    Thanks.

  2. BENN
    BENN avatar
    59 posts
    Member since:
    Dec 2011

    Posted 21 Jun in reply to BENN Link to this post

    Hi, the clicked area is actually the blank area in the grid. i've debuged the correct with both 2012 and 2016 sources, and i can make a patch that will make it work like it used to, but i don't understand why the code was changed. The 2016 code checks is the focus is within the gridview, and in that case, doesn't commit the changes.

    Why?

  3. UI for WPF is Visual Studio 2017 Ready
  4. Answer
    Yoan
    Admin
    Yoan avatar
    1066 posts

    Posted 21 Jun Link to this post

    Hi,

    Indeed, we will commit the edit operation if RadGridView loses focus. In your scenario, when you are clicking in the blank area, the focus stays in the grid. We are using the ActionOnLostFocus and its default value is CommitEdit. However, I am not sure why the behaviour is different in the 2012 version. I suppose that this was a bug which was fixed in our recent version.

    I tried to reproduce it with 2012.3.1017, but I was not able to. Which exactly is the version you are using?

    Regards,
    Yoan
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  5. BENN
    BENN avatar
    59 posts
    Member since:
    Dec 2011

    Posted 21 Jun in reply to Yoan Link to this post

    2012.3.1112.40

    It seems logical to commit edit when the cell loose focus. I'm going to write a code that will keep the old behavior.

  6. Yoan
    Admin
    Yoan avatar
    1066 posts

    Posted 21 Jun Link to this post

    Hello,

    I've checked it and it seems that this bug is reproducible with the 2012.3.1112 internal build. However, it was fixed for the next service pack - 2012.3.1129.

    If you need assistance on keeping the old behaviour, do not hesitate to contact me again.

    Regards,
    Yoan
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  7. BENN
    BENN avatar
    59 posts
    Member since:
    Dec 2011

    Posted 22 Jun Link to this post

    Well, this is the "fix" that I coded:

    private void radGrid_PreparingCellForEdit(object sender, Telerik.Windows.Controls.GridViewPreparingCellForEditEventArgs e)
    {
        var editor = e.EditingElement;
        editor.LostFocus += new RoutedEventHandler(editor_LostFocus);
    }
     
    void editor_LostFocus(object sender, RoutedEventArgs e)
    {
        (sender as FrameworkElement).LostFocus -= new RoutedEventHandler(editor_LostFocus);
        var parentDataControl = (sender as FrameworkElement).ParentOfType<GridViewDataControl>();
        var parentCell = (sender as FrameworkElement).ParentOfType<GridViewCell>();
     
        if (parentDataControl == null || parentCell == null)
            return;
     
        UIElement focusedElement = FocusManagerHelper.GetFocusedElement((DependencyObject) parentDataControl) as UIElement;
     
        CommitEditForRowsOutOfFocus(focusedElement, parentCell);
    }
     
    private void CommitEditForRowsOutOfFocus(UIElement focusedElement, GridViewCell parentCell)
    {
        if (focusedElement == null)
            return;
        List<GridViewRow> list = focusedElement.GetParents().OfType<GridViewRow>().ToList<GridViewRow>();
        foreach (GridViewRow gridViewRow in parentCell.GetParents().OfType<GridViewRow>().ToList<GridViewRow>())
        {
            if (!list.Contains(gridViewRow))
                gridViewRow.CommitEdit();
        }
    }

     

    This solution seem to work. However, even if I make a behavior out of it, I still need to go over every RadGridView on the software, and apply that behavior (And there are currently 93 grids on a lot of different views).

    I would like to avoid changing the source code of the grid, since I will then need to apply the change every time I update the source code from your website.

     

    I was hoping to use RegisterClassHandler, but unfortunately PreparingCellForEdit is not an event that can be registered in that way.

    Is there any other options?

     

    Thanks

  8. Answer
    Yoan
    Admin
    Yoan avatar
    1066 posts

    Posted 22 Jun Link to this post

    Hi,

    You can try to implement an AttachedBehavior and set it through a style for RadGridView. It would be something similar to this one:
    public class MyBehavior
        {
            private RadGridView grid = null;
     
            public MyBehavior(RadGridView grid)
            {
                this.grid = grid;
            }
     
            public static readonly DependencyProperty IsEnabledProperty
                = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(MyBehavior),
                    new PropertyMetadata(new PropertyChangedCallback(OnIsEnabledPropertyChanged)));
     
            public static void SetIsEnabled(DependencyObject dependencyObject, bool enabled)
            {
                dependencyObject.SetValue(IsEnabledProperty, enabled);
            }
     
            public static bool GetIsEnabled(DependencyObject dependencyObject)
            {
                return (bool)dependencyObject.GetValue(IsEnabledProperty);
            }
     
            private static void OnIsEnabledPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
            {
                RadGridView grid = dependencyObject as RadGridView;
                if (grid != null)
                {
                    if ((bool)e.NewValue)
                    {
                        MyBehavior behavior = new MyBehavior(grid);
                        behavior.Attach();
                    }
                }
            }
     
            private void Attach()
            {
                if (grid != null)
                {
                    grid.PreparingCellForEdit += grid_PreparingCellForEdit;
                }
            }
     
            void grid_PreparingCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
            {
                var editor = e.EditingElement;
                editor.LostFocus += new RoutedEventHandler(editor_LostFocus);
            }
     
            void editor_LostFocus(object sender, RoutedEventArgs e)
            {
                (sender as FrameworkElement).LostFocus -= new RoutedEventHandler(editor_LostFocus);
                var parentDataControl = (sender as FrameworkElement).ParentOfType<GridViewDataControl>();
                var parentCell = (sender as FrameworkElement).ParentOfType<GridViewCell>();
     
                if (parentDataControl == null || parentCell == null)
                    return;
     
                UIElement focusedElement = FocusManagerHelper.GetFocusedElement((DependencyObject)parentDataControl) as UIElement;
     
                CommitEditForRowsOutOfFocus(focusedElement, parentCell);
            }
     
            private void CommitEditForRowsOutOfFocus(UIElement focusedElement, GridViewCell parentCell)
            {
                if (focusedElement == null)
                    return;
                List<GridViewRow> list = focusedElement.GetParents().OfType<GridViewRow>().ToList<GridViewRow>();
                foreach (GridViewRow gridViewRow in parentCell.GetParents().OfType<GridViewRow>().ToList<GridViewRow>())
                {
                    if (!list.Contains(gridViewRow))
                        gridViewRow.CommitEdit();
                }
            }
        }
                 .
                 .
                 .
    <Window.Resources>
            <my:MyViewModel x:Key="MyViewModel"/>
            <Style TargetType="telerik:RadGridView">
                <Setter Property="my:MyBehavior.IsEnabled" Value="True"/>
            </Style>
        </Window.Resources>

    Let me know how this works for you.

    Regards,
    Yoan
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  9. BENN
    BENN avatar
    59 posts
    Member since:
    Dec 2011

    Posted 22 Jun in reply to Yoan Link to this post

    Thanks, it works.
Back to Top
UI for WPF is Visual Studio 2017 Ready