WPF 网格和 DataGrid 大小调整 [英] WPF Grid and DataGrid sizing

查看:45
本文介绍了WPF 网格和 DataGrid 大小调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用的 UserControl 基本上是这样布置的:

The UserControl I'm trying to work with is essentially laid out like so:

<Grid>
   <Grid.RowDefinitions>
       <Row Height="*"/>
       <Row Height="*"/>
   </Grid.RowDefinitions>
   <wpftoolkit:DataGrid x:Name="PrimaryGrid"/> <!-- ~10 rows -->
   <Border>
      <Grid>
         <Grid.RowDefinitions>
             <Row Height="*"/>
             <Row Height="*"/>
         </Grid.RowDefinitions>
         <wpftoolkit:DataGrid x:Name="SecondaryGrid"/> <!-- ~2 rows -->
         <wpftoolkit:DataGrid x:Name="OtherGrid"/> <!-- ~50 rows -->
      </Grid>
   </Border>
</Grid>

这个 UserControl 然后作为 Grid 的最后一个成员放置在我的 Window 中,这是唯一具有 Height=" 的成员*".我遇到了 DataGrid 的大小问题.

This UserControl is then placed in my Window as the last member of a Grid, the only one with Height="*". I'm having problems with the way the DataGrids are sized.

如果我在Window中将UserControlVerticalAlignment设置为Stretch,那么PrimaryGrid 获得 UserControl 的 1/2 高度,而 Border 内的两个各获得 1/4.无论每个行有多少行,它们的大小都是这样的,使 OtherGrid 的垂直空间太小,而其他的在滚动视图内则具有非行空白.

If I set VerticalAlignment of the UserControl to Stretch in the Window, then PrimaryGrid gets 1/2 height of the UserControl, and each of the two inside the Border get 1/4. They are sized like this regardless of the number of rows each have, leaving OtherGrid with too little vertical space and the others with non-row whitespace inside the scrollview.

如果我将 VerticalAlignment 设置为 Top,则网格的大小似乎与它们的内容相当,除了在 底部留下一个莫名其妙的空白>用户控件.我使用了 Snoop,并且 UserControlRowDefinition 具有正确的 ActualHeight,但 UserControl 只使用了一部分它 - 80% 左右.

If I set VerticalAlignment to Top, the grids seem to size pretty well to their contents, except there is an inexplicable whitespace being left at the bottom of the UserControl. I used Snoop and the RowDefinition for the UserControl has the proper ActualHeight, but the UserControl only uses a portion of it - 80% or so.

我真的不介意我是否修复 Stretch 案例(如何使 DataGrid 的伸展不超过其行数?)或 Top 案例(如何让 UserControl 使用它所有可用的空间?)

I don't really mind whether I fix the Stretch case (How do I make the DataGrid not stretch larger than its number of rows?) or the Top case (How do I make the UserControl use all the space it has available to it?)

推荐答案

好吧,这就是我的结果.从布局的角度来看,它主要是我想要的.后面的代码比我想要的多一点,但是哦.

Well, here's what I ended up with. It does what I want from a layout point of view, mostly. A bit more code behind than I'd like, but oh well.

在我的 DataContextChanged 事件处理程序中:

In my DataContextChanged event handler:

//for each grid
_reportObserver = new PropertyObserver<ItemCollection>(PrimaryGrid.Items)
    .RegisterHandler(c => c.Count, c => UpdateMaxHeight(PrimaryGrid));
UpdateMaxHeight(PrimaryGrid);

PropertyObserver 来自 http://joshsmithonwpf.wordpress.com/2009/07/11/one-way-to-avoid-messy-propertychanged-event-handling/

PropertyObserver is from http://joshsmithonwpf.wordpress.com/2009/07/11/one-way-to-avoid-messy-propertychanged-event-handling/

//Lots of ugly hard-coding     
private void UpdateMaxHeight(DataGrid grid)
{
   double header_height = grid.ColumnHeaderHeight;
   if (double.IsNaN(header_height))
      header_height = 22;
   double margin_height = grid.Margin.Bottom + grid.Margin.Top;
   grid.MaxHeight = header_height + margin_height + grid.Items.Count * (grid.RowHeight+2);

   UpdateLayout(); //this is key for changes to number of items at runtime
}

即使在设置了 DataGrid 的 MaxHeight 之后,情况仍然很糟糕,所以我也必须在 RowDefinition 上设置最大高度.但这仍然不对,导致上面添加了 margin_height.

Even after setting the DataGrid's MaxHeight, things were still ugly, so I had to set the max height on the RowDefinition's too. But that still wasn't right, causing the margin_height addition above.

<RowDefinition Height="*" MaxHeight="{Binding ElementName=PrimaryGrid, Path=MaxHeight}"/>

在某个时候,我会在丑陋的最大高度代码中考虑可选可见的行详细信息.

At some point, I'll take into account my optionally visible row details in my ugly max height code.

就 Top vs Stretch 而言,由于其他原因,我最终将用户控件放在 ListView 中.现在一切都很好.

As far as Top vs Stretch, I ended up for other reasons having the usercontrol in a ListView. Everything sizes nicely now.

再次感谢您查看我的问题.

Thanks again for looking at my problem.

这篇关于WPF 网格和 DataGrid 大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆