Grid.IsSharedSizeScope和ItemsControl.ItemTemplate的WPF布局问题

时间:2011-02-14 16:27:37

标签: wpf xaml layout

我正在尝试使用Grid.IsSharedSizeScope来排列GridControl中第一列中某些控件旁边的ItemsControl显示的数据绑定控件。

问题是我无法阻止控件持续垂直增长。

如何在不设置MaxHeight属性的情况下阻止他们这样做。我在各个地方尝试过不同的VerticalAlignment和VerticalContentAlignment设置,但无法弄明白。

<Grid Grid.IsSharedSizeScope="True" >
    <Grid.RowDefinitions>
        <RowDefinition SharedSizeGroup="RowOne" />
        <RowDefinition SharedSizeGroup="RowTwo" />
        <RowDefinition SharedSizeGroup="RowThree" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <SomeControl Grid.Row="0" Grid.Column="0" />
    <SomeControl Grid.Row="1" Grid.Column="0" />
    <ItemsControl Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" ItemsSource="{Binding Path=SomeSource}" ItemsPanel="{StaticResource MyHorizontalStackPanel}" >
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition SharedSizeGroup="RowOne" />
                            <RowDefinition SharedSizeGroup="RowTwo" />
                            <RowDefinition SharedSizeGroup="RowThree" />
                        </Grid.RowDefinitions>
                        <SomeControl Grid.Row="0" />
                        <SomeControl Grid.Row="1" />
                        <SomeControl Grid.Row="2" />
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>

1 个答案:

答案 0 :(得分:16)

尝试在嵌套网格上使用Grid.IsSharedSizeScope,将Grid和ItemsControl并排放在另一个有两列的网格中,好。

这是我自己愚蠢的解决办法:

<!-- outer grid (could be a stack panel) -->
<Grid Grid.IsSharedSizeScope="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <!-- header -->
    <Grid Grid.Column="0" Margin="0,10,10,0">
        <Grid.RowDefinitions>
            <RowDefinition SharedSizeGroup="RowOne" />
            <RowDefinition SharedSizeGroup="RowTwo" />
            <RowDefinition SharedSizeGroup="RowThree" />
        </Grid.RowDefinitions>
        <SomeControl Grid.Row="0" Grid.Column="0" />
        <SomeControl Grid.Row="1" Grid.Column="0" />
    </Grid>
    <!-- rows -->
    <ItemsControl Grid.Column="1" ItemsSource="{Binding Path=SomeSource}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition SharedSizeGroup="RowOne"   Height="Auto" />
                        <RowDefinition SharedSizeGroup="RowTwo"   Height="Auto" />
                        <RowDefinition SharedSizeGroup="RowThree" Height="Auto" />
                    </Grid.RowDefinitions>
                    <!-- define your row here -->
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>