4

在此处输入图像描述

简单的 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>
4

3 回答 3

1

添加一个将 Width 设置为 Auto 的 columnDefinition 以容纳 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>
于 2013-05-29T19:55:37.207 回答
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"

拥有它以使其正常工作也很重要

于 2013-06-28T10:53:32.530 回答
0

In my opinion, this is a bug that occurs when at least one of the column defintions is set to a pixel value. If you change your columns definition to a star value, then everything is fine. Check out this modified version of your code:

<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>

When you drag the grid splitter to the left side, the right column will grow somewhat indefinitely, even exceeding the MaxWidth that I restricted. If you replace Width="400" with Width="*" on the third column definition, it works correctly. Obviously, that is the only way to make it work.

于 2013-05-29T20:02:46.507 回答