GridSplitter - 无限宽度

时间:2013-05-29 19:04:25

标签: wpf

enter image description here

简单的GridSplitter在某种意义上表现得很奇怪,当我将它向左移动超过MinWidth时,另一列无限扩展。我在这里错过了什么?

<Grid>
    <Grid x:Name="holdergrid" HorizontalAlignment="Stretch">           
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width ="*" MinWidth="300"/>                
            <ColumnDefinition Width ="425"  MinWidth="300"/>
        </Grid.ColumnDefinitions>
        <Button Grid.Column="0" Content="Left"></Button>
        <Button Grid.Column="1" Content="Right"></Button>
        <GridSplitter Name="GridSplitterFolders" HorizontalAlignment="Left" Grid.Column ="1" Width ="10" ResizeBehavior="PreviousAndCurrent" />
    </Grid>
</Grid>

3 个答案:

答案 0 :(得分:1)

添加一个columnDefinition,其Width设置为Auto以容纳GridSplitter本身,并将ResizeBehavior更改为PreviousAndNext。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width ="*" MinWidth="300"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width ="425"  MinWidth="300"/>
    </Grid.ColumnDefinitions>
    <Label Content="Left" Grid.Column="0" />
    <GridSplitter HorizontalAlignment="Right" 
              VerticalAlignment="Stretch" 
              Grid.Column="1" ResizeBehavior="PreviousAndNext"
              Width="5" Background="#FFBCBCBC"/>
    <Label Content="Right" Grid.Column="2" />
</Grid>

答案 1 :(得分:1)

feO2x是对的。当GridSplitter混合了*和像素值时,会出现无限宽度。

正确的水平GridSplitter工作示例应该有:

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*" MinWidth="200"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="1*" MinWidth="200"/>
    </Grid.ColumnDefinitions>

<GridSplitter Grid.Column="1" Width="6" Style="{StaticResource gridSplitterStyleVertical}"
                HorizontalAlignment="Center"
                VerticalAlignment="Stretch"
                ResizeBehavior="PreviousAndNext"
                ResizeDirection="Columns">

注意

HorizontalAlignment="Center"

为了使其正常工作,这也很重要

答案 2 :(得分:0)

在我看来,这是一个错误,当至少有一个列定义设置为像素值时会发生这种错误。如果您将列定义更改为星形值,那么一切都很好。查看代码的修改版本:

<Window x:Class="Gridsplitter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid x:Name="Holdergrid" MaxWidth="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=ActualWidth}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" MinWidth="300" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="400" MinWidth="300" />
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0" Content="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth}"></Button>
            <Button Grid.Column="2" Content="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth}"></Button>
            <GridSplitter Name="GridSplitterFolders" HorizontalAlignment="Center" VerticalAlignment="Stretch" Grid.Column ="1" Width ="5" ResizeBehavior="PreviousAndNext"/>
        </Grid>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <TextBlock  Text="{Binding ElementName=Holdergrid, Path=ActualWidth, StringFormat=Actual Width: {0}}"></TextBlock>
            <TextBlock Text="{Binding ElementName=Holdergrid, Path=Width, StringFormat=Width: {0}}" Margin="10 0 0 0"></TextBlock>
            <TextBlock Text="{Binding ElementName=Holdergrid, Path=MaxWidth, StringFormat=Max Width: {0}}" Margin="10 0 0 0"></TextBlock>
        </StackPanel>
    </Grid>
</Window>

当你将网格分割器拖到左侧时,右列会无限增长,甚至超过我限制的MaxWidth。如果您在第三列定义中将Width="400"替换为Width="*",则它可以正常工作。显然,这是使其发挥作用的唯一方法。