Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle events generated by Grid Splitter in WPF?

Tags:

wpf

I want an event handler that handles the event when the grid splitter is being moved, Im not sure if there is one, if not, I guess I can generated an event when the size of the rows are changed?

Thanks.

like image 226
RKM Avatar asked Jul 15 '11 17:07

RKM


2 Answers

You could do the rows changing size, but GridSplitter itself is a Thumb and so has its own events such as DragStarted and DragCompleted. More details here.

Edit: If you make the GridSplitter focusable and allow it to be moved with the keyboard, read the answer by Benlitz for more information.

like image 167
Ed Bayiates Avatar answered Oct 17 '22 18:10

Ed Bayiates


I didn't tested, but I'm pretty sure that the currently accepted answer from AresAvatar won't work if you're resizing the rows/columns using keyboard arrows (by giving the focus to the grid splitter). This is a rare but possible case that you should anticipate in your application.

When the grid splitter is moved (either by drag'n'drop or using keyboard arrows), it changes the Width/Height dependency properties of the ColumnDefinitions/RowDefinitions of the grid. You can easily register a handler on this property change:

var heightDescriptor = DependencyPropertyDescriptor.FromProperty(RowDefinition.HeightProperty, typeof(ItemsControl));
heightDescriptor.AddValueChanged(myGrid.RowDefinitions[0], HeightChanged);

(This snippet will for instance track size change in the first row of the grid).

Then you can handle resize in an handler that will work in every case.

private void HeightChanged(object sender, EventArgs e)
{
    // TODO: handle row resize
}

Generally, it is really not advised to rely on the user input action (mouse dragging, keyboard inputs...) to handle a logical or visual actions/events, since there are almost always several ways to do the same actions using different inputs (mouse, keyboards, touchscreen, ease-of-use tools...).

like image 27
Benlitz Avatar answered Oct 17 '22 18:10

Benlitz