简体   繁体   English

如何在堆栈面板中固定距主窗口四个角固定距离的WPF数据网格

[英]How to anchor a wpf data grid which is inside a stack panel a fixed distance from the main window's four corners

I have the following wpf layout given to me by my employer. 我的老板给了我以下WPF布局。 It makes use of multiple nested stack panels. 它利用了多个嵌套堆栈面板。 I'm trying to anchor a data grid which is inside a stack panel a fixed distance away from the main window's four corners. 我试图将数据网格固定在堆栈面板内,该数据网格与主窗口的四个角相距固定距离。 And whenever the grid contains data of which some of it is hidden because of the parent window's size, it is supposed to display scroll bars which must disappear if not needed. 并且只要网格包含由于父窗口的大小而隐藏了某些数据的数据,就应该显示滚动条,如果不需要,滚动条必须消失。


I set the data grid's and the stack panel's width to Auto so that it fills the width and that makes the horizontal and vertical scroll bars behave as i want them to. 我将数据网格的和堆栈面板的宽度设置为“自动”,以便填充该宽度,并使水平和垂直滚动条的行为均符合我的期望。 BUT the grid doesn't have the required height. 但是网格没有所需的高度。


But when i try to set the data grid's height property to auto both the horizontal and vertical scroll bars disappear resulting in hidden data. 但是,当我尝试将数据网格的height属性设置为auto时,水平和垂直滚动条都会消失,从而导致隐藏数据。 I tried setting the data grids height property to a fixed size and updating it whenever the window is resized but the scroll bars are still hidden how can i fix this? 我尝试将数据网格的height属性设置为固定大小,并在调整窗口大小时进行更新,但滚动条仍处于隐藏状态,我该如何解决呢?

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

When you put items into a vertical StackPanel they like to pretend they have infinite vertical space. 当您将项目放入垂直StackPanel它们会假装它们具有无限的垂直空间。 Switch over to specifying rows on your Grid , and place your DataGrid in one of those rows. 切换到在Grid上指定行,然后将DataGrid放在这些行之一中。 When it's inside a Grid it knows how much space it has available and should show the scrollbars appropriately. 当它位于Grid它知道有多少可用空间,并应适当显示滚动条。

I don't understand all this StackPanel inside StackPanel stuff. 我不了解StackPanel内的所有StackPanel。 You should simply use Grid for your layout: 您应该只对布局使用Grid:

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

And having an empty StackPanel stackPanel3 as spaceholder seems awkward. 并且使用空的StackPanel stackPanel3作为空格符似乎很尴尬。 Thats what WPF elements have a Margin property for. 那就是WPF元素具有Margin属性的原因。

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

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