WPF网格和DataGrid大小 [英] WPF Grid and DataGrid sizing
问题描述
UserControl
我正在努力工作基本上是这样布置的:
< Grid>
< Grid.RowDefinitions>
< Row Height =*/>
< Row Height =*/>
< /Grid.RowDefinitions>
< wpftoolkit:DataGrid x:Name =PrimaryGrid/> <!〜〜10行 - >
< Border>
< Grid>
< Grid.RowDefinitions>
< Row Height =*/>
< Row Height =*/>
< /Grid.RowDefinitions>
< wpftoolkit:DataGrid x:Name =SecondaryGrid/> <!〜〜2行 - >
< wpftoolkit:DataGrid x:Name =OtherGrid/> <!〜〜50行 - >
< / Grid>
< / Border>
< / Grid>
这个 UserControl
然后放在我的窗口
作为 Grid
的最后一个成员,唯一一个 Height =*
。我遇到了 DataGrid
的大小问题。
如果我将 UserControl
的 VerticalAlignment
code> Stretch 在窗口
中,然后 PrimaryGrid
获得1/2高度的 UserControl
,并且 Border
中的每一个都获得1/4。它们的大小如此,无论每个行的行数如何,留下 OtherGrid
具有太小的垂直空间,而其他在滚动视图中具有非行空格。
如果我将 VerticalAlignment
设置为顶部
,网格似乎很漂亮对于他们的内容,除了在 UserControl
的底部留下一个莫名其妙的空白。我使用Snoop, RowDefinition
为 UserControl
具有正确的 ActualHeight
,但 UserControl
仅使用其中的一部分 - 80%左右。
我不确实是否修复 Stretch
case(如何使 DataGrid
不会拉伸大于其行数? )或 Top
case(如何使 UserControl
使用它可用的空间?)
嗯,这是我最后的结果。大多数情况下,它从布局的角度来看我想要的。在我的DataContextChanged事件处理程序中:
<$>
p $ p> //每个网格
_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 /
//很多丑陋的硬编码
私人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(); //这是运行时项目数量更改的关键点
}
即使设置DataGrid的MaxHeight,事情还是很丑,所以我也必须设置RowDefinition的最大高度。但是这还是不正确的,导致margin_height加上。
< RowDefinition Height =*MaxHeight ={ Binding ElementName = PrimaryGrid,Path = MaxHeight}/>
在某些时候,我将考虑到我丑陋的最大高度代码中可选择的行详细信息。
对于Top vs Stretch来说,最终,由于ListView中用户控件的其他原因。一切大小现在很好
再次感谢我的问题。
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>
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 DataGrid
s are sized.
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.
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.
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.
In my DataContextChanged event handler:
//for each grid
_reportObserver = new PropertyObserver<ItemCollection>(PrimaryGrid.Items)
.RegisterHandler(c => c.Count, c => UpdateMaxHeight(PrimaryGrid));
UpdateMaxHeight(PrimaryGrid);
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
}
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.
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屋!