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