【问题标题】:Dock a resizable wpf DataGrid at bottom of window将可调整大小的 wpf DataGrid 停靠在窗口底部
【发布时间】:2013-03-12 08:28:08
【问题描述】:

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

但是scrollviewer+datagrid的高度不会适应。

【问题讨论】:

  • 我已经尝试了很多。不会在这里列出所有内容。如果我在 DataGrid 周围放置一个 DockPanel 并设置停靠面板的高度,则在调整窗口大小时高度将保持不变。
  • 我现在添加了一些代码

标签: wpf wpfdatagrid


【解决方案1】:

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

在我的示例中,我将您的根 StackPanel 更改为 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>

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

【讨论】:

  • 谢谢!我也将 DockPanel.Dock="Top" 和 Horizo​​ntalAlignment="Left" 放在标签上!
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2011-02-13
    • 2013-11-05
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2022-08-17
    • 2019-12-29
    • 1970-01-01
    • 2017-08-17
    相关资源
    最近更新 更多