我遇到了gridsplitter的问题,我将listview推出了这个组合中的视图。重现的步骤:
对我来说,这会慢慢推动WindowView外的ListView。请注意,ScrollViewer实际上随着窗口的大小减小,但速度不同且慢慢消失。一旦滚动查看器开始滑出视图,分割器就不能再使用了!
奇怪的是,如果我不先将左侧面板最小化,我就不会出现这种情况!
对此有什么解决方法?
<Window x:Class="LayoutTest3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="800" MinHeight="600" MinWidth="800" >
<Window.Resources>
<XmlDataProvider XPath="/Celebrities/Celebrity" x:Key="celebs">
<x:XData>
<Celebrities xmlns="">
<Celebrity Name="Jimmy">
<LastName>Page</LastName>
</Celebrity>
<Celebrity Name="Johnny">
<LastName>Depp</LastName>
</Celebrity>
<Celebrity Name="Britney">
<LastName>Spears</LastName>
</Celebrity>
</Celebrities>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="NameTemplate">
<TextBlock Text="{Binding XPath=@Name}" />
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="100" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" MinWidth="400" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Background="Blue" />
<GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Center" ResizeBehavior="PreviousAndNext" VerticalAlignment="Stretch" Background="Red" />
<Border Grid.Column="2" Background="Green">
<ListView ItemsSource="{Binding Source={StaticResource celebs}}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Name" CellTemplate="{StaticResource NameTemplate}" Width="150" />
<GridViewColumn Header="LastName" DisplayMemberBinding="{Binding XPath=LastName}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Border>
</Grid>
</Window>
答案 0 :(得分:7)
GridSplitter和MinWidth列有一个已知问题,GridSplitter仍然调整了Grid的大小,忽略了Column的MinWidth。结果就是不断增长的专栏儿童获得的实际可用尺寸更大。但Grid格式的孩子考虑MinWidth,因此你的收缩列保持在MinWidth但是Growing Column的孩子超出了GridSplitter超出缩小列的MinWidth的大小。
我将在我的项目中找出如何解决这个问题,我将发布代码。同时您可以尝试以下选项。
事先知道您的网格最小尺寸应为500 +分割器尺寸,您可以设置整个网格的MinWidth。
答案 1 :(得分:2)
我是这样做的:
<Grid Name="container" SizeChanged="container_SizeChanged">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Name="rightPanel" Width="*" MinWidth="100"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Background="Red" Foreground="White">Test</TextBlock>
<GridSplitter Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Right" Width="2" Height="Auto" ResizeDirection="Columns"/>
<Border Grid.Column="1" Background="Green" Margin="0 0 0 0">
<ListView ItemsSource="{Binding Source={StaticResource celebs}}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Name" CellTemplate="{StaticResource NameTemplate}" Width="150" />
<GridViewColumn Header="LastName" DisplayMemberBinding="{Binding XPath=LastName}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Border>
</Grid>
和代码背后(哎呀!):
private void container_SizeChanged(object sender, SizeChangedEventArgs e)
{
rightPanel.MaxWidth = container.ActualWidth - 150;
}
基本上你取出最小宽度并执行类似的计算以在另一个面板上设置MaxWidth。
答案 2 :(得分:1)
也许这会有所帮助,你总是必须使用相同类型的单位作为宽度/高度,否则分割器将无法正常工作。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" MinWidth="150"/>
<ColumnDefinition Width="4*" MinWidth="150"/>
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" BorderBrush="#FF005BFF" Margin="0,0,5,0">
<ListView.View>
<GridView>
<GridViewColumn />
</GridView>
</ListView.View>
</ListView>
<GridSplitter Grid.Column="1"
HorizontalAlignment="Left" VerticalAlignment="Stretch"
Background="Black"
ShowsPreview="true"
Width="2" />
<ListView Grid.Column="1" BorderBrush="#FFFF5100" Margin="5,0,0,0">
<ListView.View>
<GridView>
<GridViewColumn />
</GridView>
</ListView.View>
</ListView>
</Grid>