Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF share column width between separate grids

I have the following setup on my WPF UserControl:

<GroupBox>   <Grid>     ...     <Grid>       <Grid.ColumnDefinitions>         <ColumnDefinition Width="Auto" />  <GroupBox>   <Grid>     <Grid>       <Grid.ColumnDefinitions>         <ColumnDefinition Width="..." /> 

I'd like the second ColumnDefinition to be the same width as the first ColumnDefinition, but I don't want to set an explicit width. Instead, I want both grids columns to automatically stretch to the width of the longest piece of content in either grid column!

Is this possible?

like image 523
devdigital Avatar asked Feb 15 '10 11:02

devdigital


People also ask

What is grid splitter in WPF?

WPF GridSplitter control is a divider that helps to split the available space into rows and columns in a grid. It supports to splits the controls horizontally or vertically with a splitter. In addition to that, it allows users to resize the grid's column width and row height on demand.

What is SharedSizeGroup in WPF?

You can use the SharedSizeGroup attribute to share column sizes across different grids. You can't normally share sizes of star-sized columns across multiple grids, however, since star sizing works only in the context of the current grid.

What is grid row in WPF?

A Grid Panel provides a flexible area which consists of rows and columns. In a Grid, child elements can be arranged in tabular form. Elements can be added to any specific row and column by using Grid.Row and Grid.Column properties. By default, a Grid panel is created with one row and one column.

How do I add a grid in WPF?

First Method: By typing XAML CodeRowDefinitions property and a ColumnDefinition Element for each column inside the Grid. ColumnDefinitions property. By default, GridLines are invisible. For showing the GridLines, set the ShowGridLines property of the Grid to True.


1 Answers

It is possible by using SharedSizeGroup. Also check out IsSharedSizeScope.

<GroupBox Grid.IsSharedSizeScope="True">   <Grid>     ...     <Grid>       <Grid.ColumnDefinitions>         <ColumnDefinition Width="Auto" SharedSizeGroup="A" />  <GroupBox>   <Grid>     <Grid>       <Grid.ColumnDefinitions>         <ColumnDefinition SharedSizeGroup="A" /> 

See here for more information.

like image 151
Lars Truijens Avatar answered Nov 12 '22 14:11

Lars Truijens