WPF GridSplitter control gives user to resize the columns width and rows height. WPF Grid divides available space in rows and columns. When we define the RowDefinitions and ColumnDefinitions, we specify how much space a particular row or column should take. A user has no option to resize width and height later.
In the below example, I have divided the Grid into two columns and fixed the width of each column to 100. Suppose we are getting the data from the database, a first column data is coming big as not expected by us. Then some data will not be visible to user.
As in the above diagram, first column data is hidden from the user. User has no option to resize the width of first column to see the full data.
How to use GridSplitter
There are two ways to use WPF GridSplitter.
- Horizontal Splitter
- Vertical Splitter
Horizontal Splitter gives user to resize rows height. For defining the GridSplitter, we must provide an extra row for splitter control between two rows. Row height for GridSplitter should be at least 5px.
For resizing the rows, we must set the splitter control HorizontalAlignment to Stretch. Below is the example.
In the above code, we have define three rows. We have taken second row for GridSplitter control. We have set the HorizontalAlignment property to Stretch. When we hover over the second row, we get a special icon with two arraows to change the height of rows.
As shown in above diagrams, we can now change the height of each row by using splitter icon.
Vertical GridSplitter allows you to change the width between two columns. For defining the splitter, we must define an extra column for GridSplitter control.
We also must set the HorizontalAlignment to Stretch of splitter control. Below is the example.
In the above code, we have defined second column for splitter control. In the GridSplitter control, we have set the HorizontalAlignment to Stretch.
As shown in above diagrams, we can now change the column width by moving the splitter icon.
GridSplitter Full Example
In the above, we have taken two GridSplitter controls for changing the rows heights and columns widht in the same grid.
In the above code, we have taken two splitter controls and for both controls set HorizontalAlignment to Stretch. Below are the screenshots.
GridSplitter is a very popular control for resizing the rows height and column widths. It gives the control the user to resize according to its own requirements. We can divide splitter control by horizontally and vertically.