WPF 网格和 DataGrid 大小调整
我尝试使用的 UserControl
基本上是这样布局的:
<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
放置在我的 Window
中,作为一个 Grid
,唯一一个带有 Height="*"
的网格。我对 DataGrid
的大小调整方式有疑问。
如果我将 UserControl
的 VerticalAlignment
设置为 Window
中的 Stretch
,则 PrimaryGrid
获取 UserControl
的 1/2 高度,Border
内的两个高度均获取 1/4。无论每个网格有多少行,它们的大小都是这样的,导致 OtherGrid
的垂直空间太小,而其他网格在滚动视图内有非行空白。
如果我将 VerticalAlignment
设置为 Top
,网格的大小似乎与它们的内容相当合适,除了在 UserControl< 的底部留下了一个莫名其妙的空白。 /代码>。我使用了 Snoop,
UserControl
的 RowDefinition
具有正确的 ActualHeight
,但 UserControl
仅使用了它 - 80% 左右。
我真的不介意是否修复 Stretch
情况(How do I make the DataGrid
not stretch more than its number of rows?) 或 Top< /code> case(如何使
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>
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?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
摘要:对 UserControl 使用
Stretch
,但对 UserControl 内的行高使用Auto
(而不是*
) 。解释:“自动”意味着< /a>:所需的空间(这是您想要的),而“*”表示:所有可用空间的比例份额(导致 1/2、1/4、1/4 分配)。
由于您希望 UserControl 使用所有可用空间,因此
Stretch
是正确的选项(这正是这个意思)。如果您希望该行占用剩余的可用空间,请将 UserControl 内的其中一个行高设置回“*”。Summary: Use
Stretch
for the UserControl, butAuto
(instead of*
) for the row heights inside your UserControl.Explanation: "Auto" means: as much space as needed (which is what you want), whereas "*" means: a proportional share of all available space (resulting in the 1/2, 1/4, 1/4-distribution).
Since you want the UserControl to use all available space,
Stretch
is the correct option (it means exactly that). Set one of the row heights inside the UserControl back to "*", if you want this row to take up the remaining available space.这是一个常见问题,您真正想要的是 2 种完全不同的布局行为:当有空间容纳所有三个时自动调整大小,* 当没有空间时调整大小。您可以尝试一些有限制的快速修复:
除此之外,您还可以尝试更复杂的操作,例如创建自定义面板(或找到其他人已经制作的面板),或者创建一个 MultiValueConverter,它可以根据高度计算每个 DG 或行的适当高度(或 MinHeight、MaxHeight)设置。 UC 和每个 DG。
This is a common problem where what you really want is 2 completely different layout behaviors: Auto sizing when there's room for all three, * sizing when there isn't. Some quick fixes you can try out with limitations:
Beyond those you can try something more complex like creating a custom Panel (or find one that someone else made already), or creating a MultiValueConverter that can calculate appropriate Height (or MinHeight, MaxHeight) settings for each DG or Row based on the height of the UC and each of the DGs.
好吧,这就是我最终得到的结果。从布局的角度来看,它基本上满足了我的要求。后面的代码比我想要的多一点,但是哦,好吧。
在我的 DataContextChanged 事件处理程序中:
PropertyObserver 来自 http://joshsmithonwpf.wordpress.com/2009/07/11/one-way-to-avoid-messy-propertychanged-event-handling/
即使设置了 DataGrid 的 MaxHeight 后,情况仍然如此丑陋,所以我也必须在 RowDefinition 上设置最大高度。但这仍然不对,导致上面添加了 margin_height 。
在某些时候,我会在丑陋的最大高度代码中考虑我可选的可见行详细信息。
就 Top 与 Stretch 而言,由于其他原因,我最终将用户控件放在 ListView 中。现在一切尺寸都很好。
再次感谢您关注我的问题。
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:
PropertyObserver is from http://joshsmithonwpf.wordpress.com/2009/07/11/one-way-to-avoid-messy-propertychanged-event-handling/
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.
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.
只是想跟进这个问题。随着时间的推移,我上面提供的解决方案并没有达到用户的期望。我现在已经更改为这样的方案:
在我使用此习惯用法的另一个地方,我将 Resizer 设置为将 MaxHeight 绑定到 ScrollViewer 的 ActualHeight 以防止其失控。这种设计可能会与整体滚动条以及 DataGrid 中的滚动条有点混淆,但是有了良好的边框和边距,还不错。
Just wanted to follow up on this problem. Over time, the solution I provided above just did not meet users expectations. I've now changed to a scheme like this:
In another place where I've used this idiom, I've set the Resizer to have a MaxHeight bound to the ScrollViewer's ActualHeight to keep it from going out of control. This design can be a little confusing with the overall scrollbar, plus scrollbars in the DataGrid, but with good borders and margins, it's not too bad.