如何在WPF中在运行时调整控件大小

时间:2014-02-24 03:20:33

标签: c# wpf xaml

DockPanel有三个子控件以水平方式并排放置: 1)TreeView 2)RichTextBox 3)网格

RichTextBox位于TreeView和Grid的中间。我将RichTextBox作为DockPanel的最后一个子项,并将LastChildFill属性设置为True。既然Grid可以在运行时关闭,我希望RichTextBox占用Grid关闭后变空的所有空间。但是如果再次显示Grid,RichTextBox应该从右侧收缩以允许Grid适合。如何实现这一目标?我是WPF的新手。另外,如何隐藏网格?这是XAML。

    <DockPanel Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" LastChildFill="True">
        <Border BorderThickness="1" DockPanel.Dock="Left" Height="Auto" HorizontalAlignment="Stretch" Margin="1" VerticalAlignment="Stretch" Width="Auto" CornerRadius="0" BorderBrush="#FF646464">
            <TreeView Name="TV" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
        </Border>
        <Border Name="Notification_Pane"  BorderThickness="1" DockPanel.Dock="Right" Height="Auto" HorizontalAlignment="Stretch" Margin="1" VerticalAlignment="Stretch" Width="Auto" CornerRadius="0" BorderBrush="#FF646464">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Label Content="Notification" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Background="LemonChiffon" Grid.Row="0" Grid.Column="0"/>
                <Button Name="btn_Close" Content="X" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="LemonChiffon" Height="Auto" Width="Auto" Grid.Row="0" Grid.Column="1" Padding="10,0,10,0" BorderThickness="0" Cursor="Hand" Focusable="True" IsHitTestVisible="True" ClickMode="Release" Click="Button_Click" />
                <ScrollViewer Height="Auto" Name="ScrollViewer" Width="Auto" Margin="0" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
                    <StackPanel CanVerticallyScroll="True" Height="Auto" Name="Notification_Panel" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

                    </StackPanel>
                </ScrollViewer>
            </Grid>
        </Border>
        <Border BorderThickness="1" Height="Auto" HorizontalAlignment="Stretch" Margin="1" VerticalAlignment="Stretch" Width="Auto" CornerRadius="0" BorderBrush="#FF646464">
            <RichTextBox Name="rtb" Height="Auto" Width="Auto" Grid.Row="2" HorizontalAlignment="Stretch" Grid.Column="1" Margin="0" />
        </Border>
    </DockPanel>

1 个答案:

答案 0 :(得分:2)

您可以使用Grid.Visibility属性来显示和隐藏网格。 以下代码应该完成这项工作:

    private void Button_Click(object sender, RoutedEventArgs e) //X Button click event.
    {
        //grid is the name of our Grid Control we want to hide.
        grid.Visibility = System.Windows.Visibility.Collapsed;
    }

再次显示网格,您应该使用以下代码:

grid.Visibility = System.Windows.Visibility.Visible;

RichTextBox控件将始终适合DockPanel控件。