Clicking on disabled cell still has some effect

0 Answers 99 Views
GridView
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Eldoir asked on 24 Jun 2022, 07:51 AM | edited on 24 Jun 2022, 09:30 AM

Hello,

My question is: how to prevent click on disabled cells?

I have some disabled cells in my RadGridView (their "IsEnabled" property is set to false), yet when I click on them, it actually adds a border around the very first cell of the corresponding row, setting it in an "unfocused" state:

On this screenshot, I clicked in the bottom right cell and it set a white border around test2.
How to avoid this? In my opinion, clicking on a disabled cell should have no effect at all.

Thanks,
Arthur

Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
commented on 24 Jun 2022, 09:32 AM | edited

I could solve my issue by doing the following, but this is obviously not ideal, it's boilerplate code. Still looking for a clean solution
public sealed class PreventDisableCellsClickBehavior : Behavior<RadGridView>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.AddHandler(UIElement.PreviewMouseLeftButtonDownEvent, (MouseButtonEventHandler)OnPreviewMouseLeftButtonDown, true);
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.RemoveHandler(UIElement.PreviewMouseLeftButtonDownEvent, (MouseButtonEventHandler)OnPreviewMouseLeftButtonDown);
    }

    private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs args)
    {
        if (args.OriginalSource is GridViewCellsPanel panel)
        {
            args.Handled = true;
        }
    }
}
Dilyan Traykov
Telerik team
commented on 28 Jun 2022, 02:56 PM

Thank you for the provided image and for sharing your solution with the community.

The behavior you describe is expected since when the IsEnabled property of the cell is False, it does not register the MouseLeftButtonDown event. The event is then passed to the underlying GridViewRow instance. The row itself has a handler for the MouseLeftButtonDown event which in turn calls the MakeFirstEnabledCellCurrent method. The method, as the name suggests is meant to make the first enabled cell current and this behavior is by design.

With this said, the approach you've come up with seems like a viable solution as the OriginalSource of the event should rarely be the GridViewCellsPanel (one such case being when the cell does not handle the event) in which case the click can be handled.

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or