繁体   English   中英

如何使StackPanel用WPF中的高度填充容器

[英]How to make StackPanel to fill his container with and height in WPF

我真的是WPF的新手,我正在尝试制作一个将在其中显示Word,PDF和Excel文件的应用程序。 窗口的左侧将显示我的应用程序中已上传文件的内容,右侧应在ScrollViewer中具有所有已上传文件的列表,以便用户选择要显示的内容。

一切都响应并正确调整为不同的窗口大小,但是滚动查看器是固定的,不会调整大小。 我希望ScrollViewer始终处于容器的最大宽度和高度,以便可以根据窗口大小正确调整其大小。

在此处输入图片说明

我尝试了不同的方法,但是都失败了,现在我不知道该怎么做。

这是我的代码:

<Window x:Class="PPMGNews.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PPMGNews"
        mc:Ignorable="d"
        Title="CoolAppForNews" Height="450" Width="800" Icon="./Resources/Logoto.ico">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="2*" />
            <RowDefinition Height="2*" />
            <RowDefinition Height="2*" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="auto" />
        </Grid.ColumnDefinitions>
        <Menu Grid.Row="1" Grid.ColumnSpan="6" BorderBrush="#FFAEA6A6" BorderThickness="0,0,0,1">
            <MenuItem Header="Add">
                <MenuItem Header="_Word" x:Name="addWord" Click="AddWord_Click"/>
                <MenuItem Header="_PDF"/>
                <MenuItem Header="_Excel"/>
            </MenuItem>
            <MenuItem Header="Delete">
                <!--<MenuItem Header="_Open"/>
                <MenuItem Header="_Close"/>
                <MenuItem Header="_Save"/> -->
            </MenuItem>
            <MenuItem Header="Edit">
                <!--<MenuItem Header="_Open"/>
                <MenuItem Header="_Close"/>
                <MenuItem Header="_Save"/>-->
            </MenuItem>
        </Menu>
        <DocumentViewer x:FieldModifier="public" x:Name="docViwer" 
                        Grid.Row="2" Grid.RowSpan="4" Grid.ColumnSpan="4"
                        BorderBrush="Black" BorderThickness="1" 
                        Margin="1,2,40,1">
        </DocumentViewer>
        <Border Grid.Row="2" Grid.RowSpan="5" Grid.Column="4"
                CornerRadius="0" BorderBrush="Gray"  
                BorderThickness="2" Margin="-35,0,0,0">
        <StackPanel Grid.Row="2" Grid.RowSpan="5" Grid.Column="4" 
                    Height="auto" Width="auto">
                <Label HorizontalAlignment="Center" FontSize="17">News</Label>
                <ScrollViewer VerticalScrollBarVisibility="Visible" 
                          HorizontalScrollBarVisibility="Disabled" 
                          HorizontalAlignment="Left">
                    <StackPanel>
                        <Label>Some News</Label>
                        <Label>Some News</Label>
                        <Label>Some News</Label>
                        <Label>Some News</Label>
                        <Label>Some News</Label>
                        <Label>Some News</Label>
                        <Label>Some News</Label>
                        <Label>Some News</Label>
                    </StackPanel>
                </ScrollViewer>
        </StackPanel>
        </Border>
    </Grid>
</Window>

想法是,每当用户添加新文件时,应用程序都应在后端将内容填充到ScrollViewer中。 然后在内容上附加单击事件以更改DocumentViewer文档。

您应该在要停靠在其各自父控制器内的控件上使用。

HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM