【问题标题】:Stretch panel with splitter带分离器的拉伸板
【发布时间】:2012-03-28 17:18:46
【问题描述】:

我想用三个面板和两个拆分器(水平拆分器和垂直拆分器)实现一个基本的 WPF 布局。

左侧和底部的两个面板必须是可调用的,一个面板必须相应地拉伸。

这是一个简单的 XAML:

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="5"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <StackPanel Background="Aqua" Grid.Column="0" Name="leftPanel" >
                <TextBlock FontSize="35" Foreground="#58290A" TextWrapping="Wrap">Left Hand Side</TextBlock>
            </StackPanel>

            <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch"/>

            <Grid Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="5" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <Label Content="... Clien Area .. Has to Stretch vertically and horizontally" Margin="10"></Label>
                    <Button Click="LeftButton_Click" Margin="10">Close Left Panel</Button>
                    <Button Click="BottomButton_Click" Margin="10">Close Bottom Panel</Button>
                </StackPanel>
                <GridSplitter Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch"/>
                <ListBox Grid.Row="2" Background="Violet" Name="bottomPanel">
                    <ListBoxItem>Hello</ListBoxItem>
                    <ListBoxItem>World</ListBoxItem>
                </ListBox>
            </Grid>
        </Grid>

和代码隐藏:

    private void LeftButton_Click(object sender, RoutedEventArgs e)
    {
        leftPanel.Visibility = (leftPanel.Visibility == System.Windows.Visibility.Visible)? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
    }

    private void BottomButton_Click(object sender, RoutedEventArgs e)
    {
        bottomPanel.Visibility = (bottomPanel.Visibility == System.Windows.Visibility.Visible) ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
    }

此代码无法按预期工作 :(。周围有 WPF 专家吗?建议同时拥有客户区(拉伸)和拆分器的解决方案?

DockPanel 可以完美运行,但我需要拆分器!

谢谢。

【问题讨论】:

    标签: wpf wpf-controls


    【解决方案1】:

    要使列折叠,您需要将包含折叠面板的ColumnDefintionWidth 更改为Auto

    【讨论】:

    • 感谢您快速正确的回答。它有效,但有一个错误。如果我触摸(调整拆分器大小)它会立即停止工作。我认为原因是 width 属性正在从 Auto 变为某种大小,那么,有什么优雅的解决方案可以将 width 换回 Auto 吗?
    • 当我做这样的事情时,我也会隐藏 GridSplitter 和它的列,当一侧折叠时没有意义。
    猜你喜欢
    • 2013-09-22
    • 2011-01-18
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多