如果运行此代码,然后单击选项卡2,然后再回到选项卡1,则应用程序将变得疯狂,并开始来回跳动列宽。对于如何解决这个问题,有任何的建议吗?

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Height="350"
    Width="525">
<Grid IsSharedSizeScope="True">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto"
                          SharedSizeGroup="Col3" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <TextBlock Text="Label 1"
               Grid.Row="0"
               Grid.Column="0" />

    <TextBox Grid.Column="1"
             Grid.Row="0"
             Text="TextBox 1" />

    <TextBlock Text="Label 2"
               Grid.Row="0"
               Grid.Column="2" />

    <TextBox Grid.Column="3"
             Grid.Row="0"
             Text="TextBox 2" />

    <DockPanel Grid.Row="1"
               Grid.Column="2"
               Grid.ColumnSpan="2">

        <TabControl>
            <TabItem Header="Tab 1">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition SharedSizeGroup="Col3" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Tab 1: Short Text.."
                               Grid.Row="0"
                               Grid.Column="0" />

                </Grid>
            </TabItem>
            <TabItem Header="Tab 2">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition SharedSizeGroup="Col3" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Tab 2: Short Text.."
                               Grid.Row="0"
                               Grid.Column="0" />
                    <TextBlock Text="Tab 2: Long Text..................................... "
                               Grid.Row="1"
                               Grid.Column="0" />
                </Grid>
            </TabItem>
        </TabControl>
    </DockPanel>
    </Grid>
</Window>


这是从类似的应用程序中剥离出来的,并且大大简化了。问题的根源是SharedSizeGroup“ Col3”。在实际的应用程序中,还有其他项目共享该列,因此除非有另一种方法来实现所需的行为,否则我无法删除SharedSizeGroup。

最佳答案

将父元素(Grid.IsSharedSizeScope)上的附加属性TabControl设置为True会停止大小共享在层次结构上方的传播。像这样:

<TabControl Grid.IsSharedSizeScope="True">


TabControl中,尺寸将对齐。

10-06 08:10