如何让WPF GridSplitter控件在ItemsControl中工作?

时间:2009-10-15 15:20:19

标签: itemscontrol gridsplitter

任何人都可以向我解释为什么以下简单示例有效:

<ItemsControl x:Class="UserControl1"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="5" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid Background="Yellow" />

        <GridSplitter Grid.Row="1"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Stretch" />

        <Grid Grid.Row="2"
              Background="Orange" />
    </Grid>
</ItemsControl>

...但是当我将ItemsPanelTemplate作为主要部分时,它不会:

<ItemsControl x:Class="UserControl1"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="5" />
                    <RowDefinition />
                </Grid.RowDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <Grid Background="Yellow" />

    <GridSplitter Grid.Row="1"
                  HorizontalAlignment="Stretch" 
                  VerticalAlignment="Stretch" />

    <Grid Grid.Row="2" Background="Orange" />
</ItemsControl>

它们都在橙色盒子的顶部显示一个黄色框,它们之间有一个水平分割器。在第一个示例中,拆分器正常工作,允许您调整两个区域的大小。在第二个例子中(产生一个几乎相同的可视树),分割器被锁定,它不允许我拖动它来调整两个区域的大小!

这是我想要实现的一个非常简化的示例 - 但它演示了我在实际应用中遇到的问题。我必须遗漏一些东西,是什么阻止了分裂器的运转?所有三个孩子都被添加到ItemsPanelTemplate网格ok ....

非常感谢任何解释或修复!

此致 戴夫

1 个答案:

答案 0 :(得分:3)

哦,没有人回复?这是因为GridSplitter必须是父Grid的直接子节点才能知道列的实际大小。

相关问题