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
118 views
in Technique[技术] by (71.8m points)

c# - SizeToContent not respecting SharedSizeGroups

Using a window's SizeToContent="WidthAndHeight" property seems to break a grid's column and row definition's property SharedSizeGroup.

<Window x:Class="SizeTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow"
        SizeToContent="WidthAndHeight">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" SharedSizeGroup="MainColumnWidth" />
            <ColumnDefinition Width="*" SharedSizeGroup="MainColumnWidth" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="*" SharedSizeGroup="MainRowHeight" />
            <RowDefinition Height="*" SharedSizeGroup="MainRowHeight" />
        </Grid.RowDefinitions>

        <Border BorderThickness="1" BorderBrush="Black" Grid.Column="0" Grid.Row="0" >
            <Button Width="100" Height="100" Margin="10" Content="A" />
        </Border>

        <Border BorderThickness="1" BorderBrush="Black" Grid.Column="1" Grid.Row="0" >
            <Button Width="100" Height="200" Margin="10" Content="B" />
        </Border>

        <Border BorderThickness="1" BorderBrush="Black" Grid.Column="0" Grid.Row="1" >
            <Button Width="200" Height="100" Margin="10" Content="C" />
        </Border>

        <Border BorderThickness="1" BorderBrush="Black" Grid.Column="1" Grid.Row="1" >
            <Button Width="100" Height="100" Margin="10" Content="D" />
        </Border>
    </Grid>
</Window>

For example, the code above produces the following:

Actual Output

However, I would expect it to produce similar to

Expected output

Where it sizes to the components but respects the shared group size.

Resizing instantly applies the shared group sizes but then you have to manually guess where the correct size is.

Is there any way to fix this?

If it makes any difference, I am using .NET 5.


Edit

Adding Grid.IsSharedSizeScope="True" to the window or the grid initially appears to solve the issue, but when resizing the window, the grid no longer fills the window.

Needs to resize grid

I suspect that what is happening is that without IsSharedSizeScope, the columns / rows are only the same size due to the Column / Row definition saying width / height is * but when IsSharedSizeScope is true, then the shared size scope come into effect, but for some reason stop the grid resizing when there is space for it to grow.

question from:https://stackoverflow.com/questions/65643487/sizetocontent-not-respecting-sharedsizegroups

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

1 Answer

0 votes
by (71.8m points)

Looks like you need to use UniformGrid

    <UniformGrid Rows="2" Columns="2">
        <Border BorderThickness="1" BorderBrush="Black">
            <Button Width="100" Height="100" Margin="10" Content="A" />
        </Border>

        <Border BorderThickness="1" BorderBrush="Black">
            <Button Width="100" Height="200" Margin="10" Content="B" />
        </Border>

        <Border BorderThickness="1" BorderBrush="Black">
            <Button Width="200" Height="100" Margin="10" Content="C" />
        </Border>

        <Border BorderThickness="1" BorderBrush="Black">
            <Button Width="100" Height="100" Margin="10" Content="D" />
        </Border>
    </UniformGrid>

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

...