跨多个网格的 WPF Grid.IsSharedSizeScope

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29125510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:01:05  来源:igfitidea点击:

WPF Grid.IsSharedSizeScope across multiple grids

c#wpfxamlissharedsizescope

提问by denver

I have a situation where I have buttons in multiple grids and the requirement that all the buttons are the same size. I attempted to use Grid.IsSharedSizeScope but was unsuccessful.

我有一种情况,我在多个网格中有按钮,并且要求所有按钮的大小相同。我尝试使用 Grid.IsSharedSizeScope 但没有成功。

The final layout should look like the following image, except all buttons should be the same size. enter image description here

最终布局应如下图所示,但所有按钮的大小应相同。 在此处输入图片说明

The XAML currently looks like this. Does anyone see where I am going wrong?

XAML 目前看起来像这样。有没有人看到我哪里出错了?

<UserControl x:Class="UserControls.UserControl2"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid Grid.IsSharedSizeScope="True">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <GroupBox Grid.Row="0" Grid.Column="0" Header="Header 1" Grid.IsSharedSizeScope="True">
        <Grid Grid.IsSharedSizeScope="True">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
                <ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
                <ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
            </Grid.ColumnDefinitions>
            <Button Content="A" Grid.Row="0" Grid.Column="0" Margin="2" />
            <Button Content="B" Grid.Row="0" Grid.Column="1" Margin="2" />
            <Button Content="C" Grid.Row="0" Grid.Column="2" Margin="2" />
        </Grid>
    </GroupBox>
    <GroupBox Grid.Row="1" Grid.Column="0" Header="Header 2" Grid.IsSharedSizeScope="True">
        <Grid Grid.IsSharedSizeScope="True">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
                <ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
            </Grid.ColumnDefinitions>
            <Button Content="AA" Grid.Row="0" Grid.Column="0" Margin="2" />
            <Button Content="BB" Grid.Row="0" Grid.Column="1" Margin="2" />
        </Grid>
    </GroupBox>
</Grid>

回答by Ayyappan Subramanian

Apply the Grid.IsSharedSizeScope only to top level container. refer below code.

仅将 Grid.IsSharedSizeScope 应用于顶级容器。参考下面的代码。

<Grid Grid.IsSharedSizeScope="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <GroupBox Grid.Row="0" Grid.Column="0" Header="Header 1" >
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
                </Grid.ColumnDefinitions>
                <Button Content="A" Grid.Row="0" Grid.Column="0" Margin="2" />
                <Button Content="B" Grid.Row="0" Grid.Column="1" Margin="2" />
                <Button Content="C" Grid.Row="0" Grid.Column="2" Margin="2" />
            </Grid>
        </GroupBox>
        <GroupBox Grid.Row="1" Grid.Column="0" Header="Header 2" >
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
                </Grid.ColumnDefinitions>
                <Button Content="AA" Grid.Row="0" Grid.Column="0" Margin="2" />
                <Button Content="BB" Grid.Row="0" Grid.Column="1" Margin="2" />
            </Grid>
        </GroupBox>
    </Grid>