隐藏 WPF 网格列时的 Gridsplitter 行为

发布于 2024-11-27 20:39:10 字数 2245 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉薄对峙 2024-12-04 20:39:10

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

<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);
    }
}

Probably the easiest thing for you here is just to set Grid.ZIndex="2" for Border1 and then toggle the ColumnSpan between 1 and 3 in the click event.

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

Code behind

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);
    }
}
江心雾 2024-12-04 20:39:10

另一个解决方案是将网格分隔符放在要调整其内容后面大小的同一列中,并为内容设置边距。请参阅此链接以获取参考解决方案: 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

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

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

Read more: How to Use a Gridsplitter in WPF | eHow.com http://www.ehow.com/how_4546867_use-gridsplitter-wpf.html#ixzz1mXqi6sGa

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文