如何锚定位于堆栈面板内的 wpf 数据网格,该网格与主窗口的四个角保持固定距离



我有以下雇主给我的WPF布局。它利用多个嵌套堆栈面板。我正在尝试将一个固定距离距离主窗口的四个角落的固定距离内的堆栈面板内的数据网格锚定。每当网格包含由于父窗口的大小而隐藏的数据时,它应该显示滚动条,如果不需要,必须消失。


我将数据网格和堆栈面板的宽度设置为自动,以使其填充宽度,并使水平和垂直滚动条的表现如我所愿。但是网格没有所需的高度。


但是,当我尝试将数据网格的高度属性设置为自动化水平和垂直滚动条时,会导致隐藏的数据。我尝试将数据网格属性设置为固定尺寸并在调整窗口大小时对其进行更新,但仍然隐藏了滚动条,我该如何修复?

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="441" d:DesignWidth="300">
    <Grid>
        <StackPanel Margin="0" Name="stackPanel1">
            <ListBox Height="100" Name="listBox1" Width="253" />
            <Button Content="Button" Height="23" Name="button1" Width="256" Click="button1_Click" />
            <StackPanel Name="stackPanel2">
                <StackPanel Height="34" Name="stackPanel3" Width="249" />
                <DataGrid AutoGenerateColumns="False" Name="dataGrid1" Height="282" />
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfApplication1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="502" Width="525" StateChanged="Window_StateChanged">
    <Grid Margin="0">
        <my:UserControl1 x:Name="userControl11" Loaded="userControl11_Loaded" />
    </Grid>
</Window>

我不明白stackpanel东西中的所有这些stackpanel。您应该简单地将网格用于布局:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <ListBox Grid.Row="0" Height="100" Name="listBox1" Width="253" />
    <Button Grid.Row="1" Content="Button" Height="23" Name="button1" Width="256" />
    <StackPanel Grid.Row="2" Height="34" Name="stackPanel3" Width="249" />
    <DataGrid Grid.Row="3" AutoGenerateColumns="False" Name="dataGrid1" />
</Grid>

并有一个空的stackpanel stackPanel3作为空间持有人似乎很尴尬。那就是WPF元素具有的保证金属性。

当您将项目放入垂直的StackPanel中时,他们喜欢假装它们具有无限的垂直空间。切换到Grid上指定行,然后将DataGrid放在其中一个行中。当它在Grid内时,它知道它拥有多少空间,应该适当地显示滚动条。

最新更新