如何防止网格分割器在窗口边界外调整列的大小?

时间:2010-07-02 17:11:19

标签: wpf gridsplitter

我有下面显示的XAML(例如)。如果将网格分割器拖到左侧,并继续拖动鼠标,右侧列的大小将超出窗口边界 - 显然不是我想要的。

问题是我无法在右侧列上设置硬MaxWidth,因为用户可以调整窗口大小,从而增加该列的可用空间。 到目前为止,我认为我需要将右侧列的MaxWidth绑定到窗口的客户区域减去左侧的MinWidth加上拆分器列的宽度。但是如果可能的话,我想避免这种情况。想法?

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="450"
        Height="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="100" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="200" MinWidth="200" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0">
            <Button>Monkey</Button>
        </Grid>

        <GridSplitter Grid.Column="1" Width="7" ResizeBehavior="PreviousAndNext" />

        <Grid Grid.Column="2" Margin="4">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <GroupBox Grid.Row="0" Header="Spaghetti" Margin="0, 0, 0, 5">
                <ComboBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch">Noodles</ComboBox>
            </GroupBox>

            <Expander Grid.Row="1" Header="Batman" IsExpanded="True" Margin="0, 0, 0, 5">
                <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch">Batman!</Button>
            </Expander>
        </Grid>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。也许这将是一个部分答案。我将列的MaxWidth限制为我正在扩展到其网格的ActualWidth。目标是分离器永远不会超过其网格的大小。绑定工作正常,但它没有实现目标,因为一旦我的网格分割器到达网格的边缘,网格开始调整大,因为我拖动分割器。如果我们可以使用拆分器调整网格大小,这应该可行。