This is a migrated thread and some comments may be shown as answers.

Commit edit when clicking on area outside of the GridView

7 Answers 324 Views
GridView
This is a migrated thread and some comments may be shown as answers.
BENN
Top achievements
Rank 1
BENN asked on 20 Jun 2016, 12:11 PM

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.

7 Answers, 1 is accepted

Sort by
0
BENN
Top achievements
Rank 1
answered on 21 Jun 2016, 08:41 AM

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?

0
Accepted
Yoan
Telerik team
answered on 21 Jun 2016, 11:26 AM
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.
0
BENN
Top achievements
Rank 1
answered on 21 Jun 2016, 01:14 PM

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.

0
Yoan
Telerik team
answered on 21 Jun 2016, 01:50 PM
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.
0
BENN
Top achievements
Rank 1
answered on 22 Jun 2016, 06:46 AM

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

0
Accepted
Yoan
Telerik team
answered on 22 Jun 2016, 01:52 PM
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.
0
BENN
Top achievements
Rank 1
answered on 23 Jun 2016, 04:36 AM
Thanks, it works.
Tags
GridView
Asked by
BENN
Top achievements
Rank 1
Answers by
BENN
Top achievements
Rank 1
Yoan
Telerik team
Share this question
or