Let's first look at a problem we often encounter when using gridsplitter:
Divide a form into two parts. The two parts are in the middle of a gridsplitter to support width changes between the left and right parts. Similar to the following:
The red in the middle is the split line. This split line supports the width change of the left and right sides. Generally, we will write this requirement in a way similar to the following XAML file:
<Window
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
X: class = "wpfapplication_gridsplitter.window1"
Title = "window1" Height = "100" width = "300">
<Grid width = "Auto" Height = "Auto">
<Grid. columndefinitions>
<Columndefinition width = "*"/>
<Columndefinition width = "Auto"/>
<Columndefinition width = "5 *"/>
</Grid. columndefinitions>
<Grid. rowdefinitions/>
<Stackpanel grid. Column = "0" background = "darkblue"/>
<Gridsplitter grid. Column = "1" width = "3" background = "red"/>
<Stackpanel grid. Column = "2" background = "# ffaea130"/>
</GRID>
</WINDOW>
But only the above Code, when we drag the split line, we will find that the size on the right side of the split line has changed, while the size on the left side has not changed, similar to the following:
A blank area appears in the middle.
Why ??
The reason is simple. The default resizebehavior attribute of gridsplitter is: basedonalignment.
The resizebehavior attribute is an enumeration type: gridresizebehavior, which contains the following enumeration items:
Based on the verticalignment and horizontalalignment attributes, basedonalignment determines which items are moved after the split line is moved.
Currentandnext grid the current region and the next region are affected.
Previusandcurrent the previous and current regions are affected
Previusandnext the first and last regions are affected.
We can change this XAML file to the following method to achieve linkage between the two sides. Note that only the bold and red attributes are modified.
<Window
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
X: class = "wpfapplication_gridsplitter.window1"
Title = "window1" Height = "100" width = "300">
<Grid width = "Auto" Height = "Auto">
<Grid. columndefinitions>
<Columndefinition width = "*"/>
<Columndefinition width = "Auto"/>
<Columndefinition width = "5 *"/>
</Grid. columndefinitions>
<Grid. rowdefinitions/>
<Stackpanel grid. Column = "0" background = "darkblue"/>
<Gridsplitter grid. Column = "1" width = "3" background = "red"
Resizebehavior = "previusandnext"/>
<Stackpanel grid. Column = "2" background = "# ffaea130"/>
</GRID>
</WINDOW>
After modification:
Summary:
The default resizebehavior attribute of gridsplitter makes it impossible for us to implement left-right Association. We only need to modify this attribute to implement left-right association.
This problem was the first time I came into contact with WPF. It took me several weeks to find out that it was caused by a simple problem. So I want to make a close-up on this blog to help people who have encountered this problem.
References:
Msdn gridresizebehavior Enumeration