4

我对 WPF 很陌生,所以如果这些天这是“老帽子”,请原谅......已经在网络/论坛上搜索过,但还没有找到我需要的答案:

我有一个包含 5 列的 WPF 网格 - 三列用于数据,两列用于 gridsplitters,这(感谢本网站上的信息!)似乎可以正常工作并调整大小。但是 - 我需要能够显示/隐藏中间列。我可以这样做,但是当我隐藏中间列时,左侧的 gridsplitter 仍然会影响“隐藏”列 - 我需要有效地在 2 和 3 列之间切换。这是我的(原型)代码:

<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) 处理 butHide.Click

    If butHide.Content = "hide" Then

        butHide.Content = "显示"
        Col2.Width = 新 GridLength(0)
        Border2.Visibility = System.Windows.Visibility.Collapsed
        GridSplitter2.Visibility = System.Windows.Visibility.Collapsed

    别的()

        butHide.Content = "隐藏"
        Col2.Width = 新 GridLength(1, GridUnitType.Star)
        Border2.Visibility = System.Windows.Visibility.Visible
        GridSplitter2.Visibility = System.Windows.Visibility.Visible

    万一
结束子

4

2 回答 2

6

可能对您来说最简单的事情就是在点击事件中设置Grid.ZIndex="2"然后Border1ColumnSpan1 和 3 之间切换。

<Border Grid.Column="0"
        Grid.ZIndex="2"
        Name="Border1"
        .../>

背后的代码

private void butHide_Click(object sender, RoutedEventArgs e)
{
    if (butHide.Content.ToString() == "hide")
    {
        butHide.Content = "show";
        Grid.SetColumnSpan(Border1, 3);
    }
    else
    {
        butHide.Content = "hide";
        Grid.SetColumnSpan(Border1, 1);
    }
}
于 2011-08-02T11:19:47.063 回答
2

另一种解决方案是将网格拆分器放在要在其内容后面调整大小的同一列中,并为内容设置边距。请参阅此链接以获取参考解决方案:http ://www.ehow.com/how_4546867_use-gridsplitter-wpf.html

    <Grid>
     <Grid.RowDefinitions>
      <RowDefinition></RowDefinition>
     </Grid.RowDefinitions>
     <Grid.ColumnDefinitions>
      <ColumnDefinition></ColumnDefinition>
      <ColumnDefinition></ColumnDefinition>
     </Grid.ColumnDefinitions>

     <GridSplitter Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" Width="4" Background="Yellow"/>
     <TextBlock Grid.Row="0" Grid.Column="0" Margin="0 0 4 0" Background="LightGray">Text Block</TextBlock>
     <TextBlock Grid.Row="0" Grid.Column="1" Background="LightGreen">Text Block 2</TextBlock>
     </Grid>

阅读更多:如何在 WPF 中使用 Gridsplitter | eHow.com http://www.ehow.com/how_4546867_use-gridsplitter-wpf.html#ixzz1mXqi6sGa

于 2012-02-16T11:57:53.490 回答