Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

wpf - Grid's SharedSizeGroup and * sizing

I have a user control, call it UserControl, that has a grid with the following column definitions:

<Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
            <ColumnDefinition Width="Auto" SharedSizeGroup="B"/>
            <ColumnDefinition Width="*" SharedSizeGroup="C"/>
            <ColumnDefinition Width="Auto" SharedSizeGroup="D"/>
            <ColumnDefinition MinWidth="30" Width="*" SharedSizeGroup="E"/>
            <ColumnDefinition MinWidth="30" Width="*" SharedSizeGroup="F"/>
            <ColumnDefinition Width="110" SharedSizeGroup="G"/>
            <ColumnDefinition MinWidth="30" Width="Auto" SharedSizeGroup="H"/>
            <ColumnDefinition MinWidth="30" Width="Auto" SharedSizeGroup="I"/>
            <ColumnDefinition MinWidth="30" Width="Auto" SharedSizeGroup="J"/>
            <ColumnDefinition MinWidth="30" Width="Auto" SharedSizeGroup="K"/>
            <ColumnDefinition MinWidth="30" Width="Auto" SharedSizeGroup="L"/>
</Grid.ColumnDefinitions>

I also have MainWindow, which contains a grid itself, with the following property defined on the grid:

Grid.IsSharedSizeScope="True"

Now, I added a couple of UserControls to the grid in MainWindow (each to a separate row). My goal is to have each of the column widths of the different UserControls to remain in sync. Everything works fine when using SharedSizeGroup except for one thing. It seems that any column with a Width of * does not behave as it should. It looks like the * column widths are set as if they were Auto instead.

Are there any limitations/issues with SharedSizeGroup and * sizing? This seems like the best way to keep the column widths in sync but I can't seem to fix this.

Thanks.

question from:https://stackoverflow.com/questions/4664008/grids-sharedsizegroup-and-sizing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

DefinitionBase.SharedSizeGroup Property (Microsoft Docs):

Columns and rows that participate in size-sharing do not respect Star sizing. In the size-sharing scenario, Star sizing is treated as Auto

If you use star then all columns would be the same width, so you should assign the same SharedSizeGroup to all if you do not mind the auto-sizing aspect:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
    <ColumnDefinition Width="Auto" SharedSizeGroup="B"/>
    <ColumnDefinition Width="*" SharedSizeGroup="StarDontWork"/>
    <ColumnDefinition Width="Auto" SharedSizeGroup="D"/>
    <ColumnDefinition MinWidth="30" Width="*" SharedSizeGroup="StarDontWork"/>
    <ColumnDefinition MinWidth="30" Width="*" SharedSizeGroup="StarDontWork"/>
    <ColumnDefinition Width="110" SharedSizeGroup="G"/>
    <ColumnDefinition MinWidth="30" Width="Auto" SharedSizeGroup="H"/>
    <ColumnDefinition MinWidth="30" Width="Auto" SharedSizeGroup="I"/>
    <ColumnDefinition MinWidth="30" Width="Auto" SharedSizeGroup="J"/>
    <ColumnDefinition MinWidth="30" Width="Auto" SharedSizeGroup="K"/>
    <ColumnDefinition MinWidth="30" Width="Auto" SharedSizeGroup="L"/>
</Grid.ColumnDefinitions>

(From this behavior it also follows that you can drop the Width property when SharedSizeGroup is set)


You can deduce that if all auto-sizing columns are in shared size groups then all star-sizing columns will have the same sizes as every grid in scope will have the same amount of unused space left that will be distributed to the star-sized columns.

A simple example:

<Grid.ColumnDefinitions>
    <ColumnDefinition SharedSizeGroup="A"/>
    <ColumnDefinition SharedSizeGroup="B" Width="*"/>
    <ColumnDefinition SharedSizeGroup="C"/>
</Grid.ColumnDefinitions>

As explained the Width="*" will not do anything, but as columns 0 and 2 are synched, column 1 has to be synched as well so you can just drop the SharedSizeGroup:

<Grid.ColumnDefinitions>
    <ColumnDefinition SharedSizeGroup="A"/>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition SharedSizeGroup="C"/>
</Grid.ColumnDefinitions>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...