无法在两个网格中匹配列大小,一个在TabControl中

时间:2014-08-07 20:52:02

标签: wpf xaml

我正在使用WPF应用程序中的对话框。该对话框包含许多嵌套Grid控件和TabControl,如下所示。

<Window . . .>
    <Grid Name="LayoutRoot>
        <Grid.RowDefinitions>
            <RowDefintion Height="*" />
            <RowDefintion Height="Auto" />
        </Grid.RowDefinitions>

        <Grid Name="Common" Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Column1" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" SharedSizeGroup="Column2" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefintions>
                <RowDefintion Height="Auto" />
                <RowDefintion Height="Auto" />
            </Grid.RowDefinitions>

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

                <TextBlock Grid.Column="2" Grid.Row="0" . . . />
                <ComboBox Grid.Column="3" Grid.Row="0". . . />

                <TabControl Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="1" Name="Tab1">
                    <TabItem Header="Tab 1">
                        <Grid Name="Tab1Grid" Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" SharedSizeGroup="Column1" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" SharedSizeGroup="Column2" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefintions>
                                . . . 
                            </Grid.RowDefinitions>
                            . . .
                        </Grid>
                    </TabItem>

                    <TabItem Header="Tab 2">
                        . . . 
                    </TabItem>
                </TabControl>
        </Grid>

        <!-- Other controls here in the second row of the outer most Grid -->
    </Grid>
</Window>

所有这一切的要点是保持包含TextBlock的列的宽度控制相同,但是,它不是那样工作的。 Width="Auto"属性似乎覆盖了SharedSizeGroup属性。如果我从Width删除Grids属性,它们就像两列中最大的字符串一样大,但这也不是我想要的。我希望Grid第一个TabControl's内的TabItem中第一列的宽度足够宽,以容纳该列中最长的字符串,然后我想要第一列中的第一列保持Grid大小相同的TabControl。第三列也是如此。

我希望这是有道理的。

可以这样做,还是我误解了SharedSizeGroup功能的工作原理?

2 个答案:

答案 0 :(得分:1)

您并没有误解SharedSizeGroup功能。但是,要使其工作,还必须定义范围,以便WPF知道在何处查找共享大小组。为此,您在容器上使用Grid.IsSharedSizeScope属性,该容器包含您使用共享大小组的所有网格。在您的示例中,它将是这个:

<Grid Name="LayoutRoot" Grid.IsSharedSizeScope="True">

请注意,此附加属性可以在任何控件类型上设置,它不一定必须是Grid。 在您的情况下,您必须要小心,因为TabControl将其整个内容推向右侧,因此您的列将不会完全对齐。您必须使用一些边距才能使其正常工作(SharedSizeGroup只会使大小相同,而不是位置。)

希望这会有所帮助,因为你的代码片段充满了拼写错误,我建议下次首先通过Visual Studio运行它...

答案 1 :(得分:0)

查看代码,看起来好像您希望父网格中的第0列与选项卡1中第0列的宽度相同。同样,父级中的第2列与选项卡1中的第2列相同。< / p>

如果是这种情况,则在父网格名称中给出第0列和第2列,然后将选项卡1中第0列和第2列的宽度绑定到相应elementName的ActualWidth属性。

<Window . . .>
    <Grid Name="LayoutRoot>
        <Grid.RowDefinitions>
            <RowDefintion Height="*" />
            <RowDefintion Height="Auto" />
        </Grid.RowDefinitions>

        <Grid Name="Common" Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" Name="ParentCol0" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" Name="ParentCol2" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefintions>
                <RowDefintion Height="Auto" />
                <RowDefintion Height="Auto" />
            </Grid.RowDefinitions>

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

                <TextBlock Grid.Column="2" Grid.Row="0" . . . />
                <ComboBox Grid.Column="3" Grid.Row="0". . . />

                <TabControl Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="1" Name="Tab1">
                    <TabItem Header="Tab 1">
                        <Grid Name="Common" Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width={Binding ActualWidth, ElementName="ParentCol0" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width={Binding ActualWidth, ElementName="ParentCol2" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefintions>
                                . . . 
                            </Grid.RowDefinitions>
                            . . .
                        </Grid>
                    </TabItem>

                    <TabItem Header="Tab 2">
                        . . . 
                    </TabItem>
                </TabControl>
        </Grid>

        <!-- Other controls here in the second row of the outer most Grid -->
    </Grid>
</Window>