WPF网格SharedSizeGroup错误

时间:2010-06-17 16:11:51

标签: .net wpf xaml

如果您运行此代码并单击选项卡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。

1 个答案:

答案 0 :(得分:3)

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

<TabControl Grid.IsSharedSizeScope="True">

TabControl范围内,尺寸将对齐。