隐藏 WPF 网格列时的 Gridsplitter 行为
我对 WPF 还很陌生,所以请原谅我,如果这是“旧帽子”这些天...已经在网络/论坛上搜寻,但还没有找到我需要的答案:
我有一个包含 5 列的 WPF 网格 -三个用于数据,两个用于网格分割器,(感谢本网站上的信息!)似乎可以正常工作并调整大小。但是 - 我需要能够显示/隐藏中间列。我可以做到这一点,但是当我隐藏中间列时,左侧 gridsplitter 仍然会影响“隐藏”列 - 我需要在 2 和 2 之间有效切换。三栏。这是我的(原型)代码:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Name="Col0" Width="*" />
<ColumnDefinition Name="Col1" Width="auto" />
<ColumnDefinition Name="Col2" Width="*" />
<ColumnDefinition Name="Col3" Width="auto" />
<ColumnDefinition Name="Col4" Width="auto" />
</Grid.ColumnDefinitions>
<GridSplitter Grid.Column="1" Height="100" HorizontalAlignment="Center" Margin="0" Name="GridSplitter1" VerticalAlignment="Stretch" Width="3" />
<GridSplitter Grid.Column="3" Height="100" HorizontalAlignment="Center" Margin="0" Name="GridSplitter2" VerticalAlignment="Stretch" Width="3" />
<Border BorderBrush="Silver" BorderThickness="1" Grid.Column="0" HorizontalAlignment="Stretch" Margin="0" Name="Border1" VerticalAlignment="Stretch" Background="#FFC84797" />
<Border BorderBrush="Silver" BorderThickness="1" Grid.Column="2" HorizontalAlignment="Stretch" Margin="0" Name="Border2" VerticalAlignment="Stretch" Background="Black" />
<Border BorderBrush="Silver" BorderThickness="1" Grid.Column="4" HorizontalAlignment="Stretch" Margin="0" Name="Border3" VerticalAlignment="Stretch" Background="#FFA60000">
<Button Content="hide" Height="33" Name="butHide" Width="85" />
</Border>
</Grid>
Private Sub butHide_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles butHide.Click If butHide.Content = "hide" Then butHide.Content = "show" Col2.Width = New GridLength(0) Border2.Visibility = System.Windows.Visibility.Collapsed GridSplitter2.Visibility = System.Windows.Visibility.Collapsed Else() butHide.Content = "hide" Col2.Width = New GridLength(1, GridUnitType.Star) Border2.Visibility = System.Windows.Visibility.Visible GridSplitter2.Visibility = System.Windows.Visibility.Visible End If End Sub
I'm pretty new to WPF, so please excuse me if this is 'old hat' these days... have trawled the web/forum and haven't quite found the answer I need:
I have a WPF grid with 5 columns - three for data, two for gridsplitters, which (thanks to info on this site!) seems to work and resize fine. However - I need to be able to show/hide the middle column. I can sort-of do this, but when I hide the middle column, the left hand gridsplitter still affects the "hidden" column - I need to effectively toggle between 2 & three colums. Here's my (prototype) code:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Name="Col0" Width="*" />
<ColumnDefinition Name="Col1" Width="auto" />
<ColumnDefinition Name="Col2" Width="*" />
<ColumnDefinition Name="Col3" Width="auto" />
<ColumnDefinition Name="Col4" Width="auto" />
</Grid.ColumnDefinitions>
<GridSplitter Grid.Column="1" Height="100" HorizontalAlignment="Center" Margin="0" Name="GridSplitter1" VerticalAlignment="Stretch" Width="3" />
<GridSplitter Grid.Column="3" Height="100" HorizontalAlignment="Center" Margin="0" Name="GridSplitter2" VerticalAlignment="Stretch" Width="3" />
<Border BorderBrush="Silver" BorderThickness="1" Grid.Column="0" HorizontalAlignment="Stretch" Margin="0" Name="Border1" VerticalAlignment="Stretch" Background="#FFC84797" />
<Border BorderBrush="Silver" BorderThickness="1" Grid.Column="2" HorizontalAlignment="Stretch" Margin="0" Name="Border2" VerticalAlignment="Stretch" Background="Black" />
<Border BorderBrush="Silver" BorderThickness="1" Grid.Column="4" HorizontalAlignment="Stretch" Margin="0" Name="Border3" VerticalAlignment="Stretch" Background="#FFA60000">
<Button Content="hide" Height="33" Name="butHide" Width="85" />
</Border>
</Grid>
Private Sub butHide_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles butHide.Click If butHide.Content = "hide" Then butHide.Content = "show" Col2.Width = New GridLength(0) Border2.Visibility = System.Windows.Visibility.Collapsed GridSplitter2.Visibility = System.Windows.Visibility.Collapsed Else() butHide.Content = "hide" Col2.Width = New GridLength(1, GridUnitType.Star) Border2.Visibility = System.Windows.Visibility.Visible GridSplitter2.Visibility = System.Windows.Visibility.Visible End If End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里对您来说最简单的事情可能就是为
Border1
设置Grid.ZIndex="2"
,然后在 1 和 3 之间切换ColumnSpan
在点击事件中。代码隐藏
Probably the easiest thing for you here is just to set
Grid.ZIndex="2"
forBorder1
and then toggle theColumnSpan
between 1 and 3 in the click event.Code behind
另一个解决方案是将网格分隔符放在要调整其内容后面大小的同一列中,并为内容设置边距。请参阅此链接以获取参考解决方案: http://www.ehow.com/how_4546867_use-gridsplitter- wpf.html
了解更多:如何在 WPF 中使用 Gridsplitter | eHow.com http://www.ehow.com/how_4546867_use-gridsplitter-wpf。 html#ixzz1mXqi6sGa
The other solution is put the grid splitter in the same column you want to resize behind its content and set margin to the content. See this link for reference solution: http://www.ehow.com/how_4546867_use-gridsplitter-wpf.html
Read more: How to Use a Gridsplitter in WPF | eHow.com http://www.ehow.com/how_4546867_use-gridsplitter-wpf.html#ixzz1mXqi6sGa