How to handle events generated by Grid Splitter in WPF?

I want an event handler that handles an event when the grid separator moves, I'm not sure if it is, if not, I think I can generate an event when the row size changes?

Thanks.

+8
source share
2 answers

You can resize the rows, but the GridSplitter itself is Thumb and therefore has its own events, such as DragStarted and DragCompleted. More details here .

Edit: If you make GridSplitter attractive and allow it to be moved from the keyboard, read Benlitz's answer for more information.

+13
source

I did not check, but I am sure that the currently accepted answer from AresAvatar will not work if you resize the rows / columns using the arrows on the keyboard (paying attention to the grid separator). This is a rare but possible case that you should foresee in your statement.

When moving the grid divider (using drag'n'drop or using the arrows on the keyboard), it changes the Width / Height dependency properties for ColumnDefinitions / RowDefinitions grid. You can easily register a handler for this property change:

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

(This fragment, for example, will resize the track in the first row of the grid).

Then you can handle the resizing in a handler that will work anyway.

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

As a rule, it is not recommended to rely on user input (drag and drop, keyboard input ...) to handle logical or visual actions / events, since there are almost always several ways to perform the same actions using different inputs (mouse, keyboard , touch screen, easy to use tools ...).

+7
source

Source: https://habr.com/ru/post/892853/


All Articles