如何使 StackPanel 在 WPF 中填充容器和高度

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

我真的是 WPF 的新手,我正在尝试制作一个可以在其中显示 Word、PDF 和 Excel 文件的应用程序。 window 的左侧将显示我的应用程序中上传文件的内容,右侧应该有一个 ScrollViewer 中所有上传文件的列表,供用户 select 要显示的内容.

Everything is responsive and resize correctly to different window size, but the scroll viewer is fixed and doesn't resize. I want my ScrollViewer to always be at max width and height of his container, so that it resizes correctly on what ever the window size is.

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

这是我的代码:

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