Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
762 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.

See Question&Answers more detail:os

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

1 Reply

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...