在WPF项目中,我想将DataGrid停靠到窗口的底部,这样如果窗口调整大小,我将能够使用更多的DataGrid.像这样:

我怎么做?我所有的DockPanel尝试都失败了.
目前的尝试是在这里:
<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>
Run Code Online (Sandbox Code Playgroud)
但是,scrollviewer + datagrid的高度不会适应.
首先,在DockPanel.Dock没有DockPanel作为父母的情况下使用并没有太大作用......
在我的例子中,我将你的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>
Run Code Online (Sandbox Code Playgroud)
最后,为了让它在所有剩余空间上真正伸展,我删除了Height你设置的属性,因为这阻止了它拉伸.