Introduction to the resizebehavior attribute of gridsplitter

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.