【问题标题】:How to make StackPanel to fill his container with and height in WPF如何使 StackPanel 在 WPF 中填充他的容器和高度
【发布时间】:2019-01-30 19:11:42
【问题描述】:

我真的是 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 文档。

【问题讨论】:

标签: c# wpf xaml wpf-controls app.xaml


【解决方案1】:

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

HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

【讨论】:

  • 嗨,它工作得很好,就像我以前习惯的那样,但是现在如果我再添加 200 个标签,它就不会显示滚动条,而且我无法在 ScrollViewer 中的所有标签之间滚动启动程序。我刚刚在 ScrollViewer 中添加了HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
  • 我很高兴它成功了。在这种情况下,您能否通过将其标记为答案来结束该主题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
  • 2010-10-04
  • 1970-01-01
  • 2022-01-20
相关资源
最近更新 更多