Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WPF: The Jumpy GridSplitter Fix

0.00/5 (No votes)
7 Mar 2016 1  
When GridSplitter stops resizing by mouse, yet operates by keyboard arrows

Introduction

Apart from Microsoft, it is common knowledge that the WPF GridSplitter has problems with mouse operation in complex layouts. Then it may succeed, if user drags it deliberately slow, but suddenly jumps back to old position on normal mouse movement. However using keyboard arrows always works trouble-free.

AFAIG (as far as I can Google), there is no real known fix, apart from using custom splitters. Until proven otherwise, I deem the following code as a universal fix for the described problem.

Code

using System;
using System.Reflection;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

private readonly MethodInfo miMoveSplitter = typeof(GridSplitter)
    .GetMethod("KeyboardMoveSplitter",BindingFlags.Instance | BindingFlags.NonPublic);

void GridSplitter_DragDelta(object sender, DragDeltaEventArgs e)
{
    if (Math.Abs(e.VerticalChange) > 10) // or e.HorizontalChange
    {
        // Splitter has stopped resizing grid rows:
        // BugFix: use keyboard for continuing resizing until user releases mouse
        miMoveSplitter.Invoke(sender, new object[]{e.HorizontalChange, e.VerticalChange});
    }
}

I assumed here a horizontal splitter resizing rows, for a vertical one test for DragDeltaEventArgs.HorizontalChange.

10 proved a good threshold value for standard DragIncrement of 1.

Explanation

In normal dragging operation, the reported change by the DragDelta event is roughly the DragIncrement value. If the threshold is exceeded, GridSplitter has stopped resizing (Grid raises no more Layout events), and cursor and splitter position will differ substantially. While the mouse is still captured, GridSplitter won't recover, until the user releases the mouse. Now, we invoke a private method used for processing keyboard input and the resizing will continue.

Points of Interest

If GridSplitter works correctly, the cursor is centered on the dragged splitter; if the fix kicks in, you can notice a slight offset.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here