在窗口底部停靠一个可调整大小的 wpf DataGrid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15356400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:01:35  来源:igfitidea点击:

Dock a resizable wpf DataGrid at bottom of window

wpfwpfdatagrid

提问by Anders Lindén

In a WPF project, I want to dock a DataGrid to the bottom of a window so that if the window resizes, I will be able to utilize more of the DataGrid. Like this:

在 WPF 项目中,我想将 DataGrid 停靠在窗口底部,以便在调整窗口大小时,我将能够利用更多的 DataGrid。像这样:

enter image description here

在此处输入图片说明

How do I do that? All my DockPanel attempts have failed.

我怎么做?我所有的 DockPanel 尝试都失败了。

The current attempt is here:

目前的尝试是在这里:

<Window x:Class="Foo.SQLDialog"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:c="clr-namespace:Foo.Controls"
   Title="test" ResizeMode="CanResize" Width="400" Height="400">
  <StackPanel Orientation="Vertical" Height="Auto" Width="Auto">
    <StackPanel Orientation="Vertical">
      <Label Content="SQL" HorizontalAlignment="Left"/>
      <TextBox Width="377" Height="100" Name="txtSQL"/>
      <Button Content="Run SQL" Click="Button_Click_1" />
    </StackPanel>
    <Label Content="Result" HorizontalAlignment="Left"/>
    <ScrollViewer Width="Auto" Height="180" DockPanel.Dock="Right,Bottom"
        ScrollViewer.CanContentScroll="True" 
        ScrollViewer.VerticalScrollBarVisibility="Auto"
        ScrollViewer.HorizontalScrollBarVisibility="Auto">
      <DataGrid x:Name="dataResult" />
    </ScrollViewer>
  </StackPanel>
</Window>

The height of the scrollviewer+datagrid will not adapt however.

然而,scrollviewer+datagrid 的高度不会适应。

回答by Blachshma

First of all, using DockPanel.Dockwithout having a DockPanel as a parent doesn't do much...

首先,在DockPanel.Dock没有 DockPanel 的情况下使用作为父级并没有多大作用......

In my example I changed your root StackPanelto a DockPanelso it will work as you want.
I also used DockPanel.LastChildFillproperty which makes sure the last child of the DockPanel will get all the remaining space:

在我的示例中,我将您的 root 更改StackPanel为 a,DockPanel以便它可以按您的意愿工作。
我还使用了DockPanel.LastChildFill属性,以确保DockPanel的最后一个子项将获得所有剩余空间:

<DockPanel LastChildFill="True">
    <StackPanel Orientation="Vertical" DockPanel.Dock="Top">
        <Label Content="SQL" HorizontalAlignment="Left"/>
        <TextBox Width="377" Height="100" Name="txtSQL"/>
        <Button Content="Run SQL" Click="Button_Click_1" />
    </StackPanel>
    <Label Content="Result" HorizontalAlignment="Left" DockPanel.Dock="Top"/>
    <ScrollViewer DockPanel.Dock="Bottom,Right"
    ScrollViewer.CanContentScroll="True" 
    ScrollViewer.VerticalScrollBarVisibility="Auto"
    ScrollViewer.HorizontalScrollBarVisibility="Auto">
        <DataGrid x:Name="dataResult"  />
    </ScrollViewer>
</DockPanel>

Finally, to make it really stretch on all the remaining space, I removed the Heightproperty you set, as this blocked it from stretching.

最后,为了让它在所有剩余空间上真正伸展,我删除了Height您设置的属性,因为这阻止了它伸展。

回答by Joel

Not sure if useful or if i understand you question the right way but have you tried this:

不确定是否有用,或者我是否理解您以正确的方式提问,但您是否尝试过:

<DataGrid DockPanel.Dock="Right, Bottom" VerticalAlignment="Bottom" HorizontalAlignment="Right" ></DataGrid>