Wpf GridSplitter-无限宽
简单的GridSplitter在某种意义上表现得很奇怪,当我将它向左移动超过MinWidth时,另一列将无限扩展。我错过了什么Wpf GridSplitter-无限宽,wpf,Wpf,简单的GridSplitter在某种意义上表现得很奇怪,当我将它向左移动超过MinWidth时,另一列将无限扩展。我错过了什么 <Grid> <Grid x:Name="holdergrid" HorizontalAlignment="Stretch"> <Grid.ColumnDefinitions> <ColumnDefinition Width ="*" MinWidth="30
<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>
添加一个宽度设置为“自动”的列定义,以容纳GridSplitter本身,并将ResizeBehavior更改为Previous和Next
<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>
在我看来,当至少有一个列定义被设置为像素值时,就会出现这种错误。如果将列定义更改为星形值,则一切正常。查看您的代码的此修改版本:
<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=“*”
,则它可以正常工作。显然,这是使其工作的唯一方法。feO2x是正确的。当GridSplitter具有混合*和像素值时,会出现无限宽度
水平网格拆分器的正确工作示例应具有:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" MinWidth="200"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*" MinWidth="200"/>
</Grid.ColumnDefinitions>
同样重要的是,要使其正常工作,这一点也很重要如果您仅使用具有星形或自动栅格长度的列定义,则可以删除栅格本身的
MaxWidth
限制。这一点很有效,但请尝试将拆分器移到左侧限制上方。。。现在,当您再次尝试移动它时,直到您首先拖动上一个超调距离,它才开始移动。这似乎是一个bug。请尝试HorizontalAlignment=“Stretch”
<GridSplitter Grid.Column="1" Width="6" Style="{StaticResource gridSplitterStyleVertical}"
HorizontalAlignment="Center"
VerticalAlignment="Stretch"
ResizeBehavior="PreviousAndNext"
ResizeDirection="Columns">
HorizontalAlignment="Center"