防止 WPF 控件扩展到可视区域之外
我的用户控件中有一个 ItemsControl
,周围有一个滚动查看器,当它变得太大时(太大,内容大于 UserControl
的可视区域)。问题在于,它所在的网格不断扩展,因此滚动查看器永远不会启动(除非我为网格指定精确的高度)。请参阅下面的代码并提前致谢。
<UserControl x:Class="BusinessObjectCreationWizard.View.TableSelectionPageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<GroupBox FontWeight="Bold" Height="300px"
Header="Tables"
Padding="2">
<ScrollViewer>
<ItemsControl FontWeight="Normal"
ItemsSource="{Binding Path=AvailableTables}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=DisplayName}"
IsChecked="{Binding Path=IsSelected}"
Margin="2,3.5" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</GroupBox>
</UserControl>
该用户控件在此处加载,
<Border Background="White" Grid.Column="1" Grid.Row="0">
<HeaderedContentControl Content="{Binding Path=CurrentPage}"
Header="{Binding Path=CurrentPage.DisplayName}" />
</Border>
我不想指定高度。
I have an ItemsControl
in my user control with a scroll viewer around it for when it gets too big (Too big being content is larger than the viewable area of the UserControl
). The problem is that the grid that it is all in just keeps expanding so that the scroll viewer never kicks in (unless I specify an exact height for the grid). See code below and thanks in advance.
<UserControl x:Class="BusinessObjectCreationWizard.View.TableSelectionPageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<GroupBox FontWeight="Bold" Height="300px"
Header="Tables"
Padding="2">
<ScrollViewer>
<ItemsControl FontWeight="Normal"
ItemsSource="{Binding Path=AvailableTables}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=DisplayName}"
IsChecked="{Binding Path=IsSelected}"
Margin="2,3.5" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</GroupBox>
</UserControl>
This user control is loaded here
<Border Background="White" Grid.Column="1" Grid.Row="0">
<HeaderedContentControl Content="{Binding Path=CurrentPage}"
Header="{Binding Path=CurrentPage.DisplayName}" />
</Border>
I would like to not specify the height.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您从 GroupBox 中删除高度(据我所知,这就是您想要做的),那么它将填充其容器,除非上游有一个面板强加了自己的尺寸规则。
我使用了 XAML 的简化版本。我删除了模板和绑定,并对一些项目进行了硬编码,以使其独立;这些更改不会影响布局的完成方式。
运行它,您将看到内容的大小确实适合窗口,并且滚动条仅在窗口太小而无法看到所有三个项目时才启用。我相信这就是你想要的。
因此,问题很可能是父面板之一,您没有在示例 XAML 中显示该面板。如果您的 GroupBox 出现在 StackPanel 内,则可能会出现您所描述的问题:
现在 GroupBox 出现在窗口的顶部,其大小完全适合其内容。如果将窗口缩小得足够大,则 GroupBox 将被切断 - 因为它的大小适合其内容,而不是其容器。这听起来像是您所描述的问题。
原因是 StackPanel 询问其子级的理想高度是多少(基于其内容),并使用该高度。如果没有 StackPanel(或类似的东西),默认情况下将遵循控件的 VerticalAlignment,如果将其设置为 Stretch 的默认值,则控件将拉伸以填充其父级。这意味着它不会比它的父级高,这听起来像你想要的。
解决方案:删除 StackPanel(或其他导致问题的内容)并使用其他内容。根据您想要完成的任务,您可能会更幸运地使用 DockPanel 或 Grid。如果不了解更多关于您的布局的信息,很难说清楚。
编辑:好的,看起来问题确实是
HeaderedContentControl
父级 - 但不是直接的。 HeaderedContentControl 不是面板,因此它自己不执行任何布局(其后代 GroupBox 不存在同样的问题)。问题在于它的默认模板——其中包括一个 StackPanel。好消息是,您可以自由地使用不同的模板,比如说使用 DockPanel 的模板:如果您省略
部分,这会重现您的问题;但有了适当的样式,它允许 GroupBox 填充其容器,因此 ScrollViewer 将在您需要时获得滚动条。If you remove the Height from your GroupBox (which, as far as I understand, is what you want to do), then it will fill its container, unless there's a panel upstream that imposes its own sizing rules.
I used this simplified version of your XAML. I removed the template and the binding, and hard-coded some items, to make this stand alone; those changes won't affect the way layout is done.
Run it, and you'll see that the content does indeed size to fit the window, and the scrollbar only enables when the window gets too small to see all three items. I believe this is what you want.
So the problem is most likely one of the parent panels, one you're not showing in your sample XAML. The problem you describe could occur if your GroupBox appears inside a StackPanel:
Now the GroupBox appears at the top of the Window, sized to exactly fit its contents. If you shrink the Window enough, the GroupBox will be cut off -- because it's sized to fit its content, not its container. This sounds like the problem you're describing.
The reason is that StackPanel asks its children what their ideal height is (based on their content), and uses that height. Without StackPanel (or something similar), the default is to respect the control's VerticalAlignment, and if that's set to the default value of Stretch, then the control is stretched to fill its parent. This means it won't be taller than its parent, which sounds like what you want.
Solution: remove the StackPanel (or whatever else is causing you problems) and use something else. Depending on what you're trying to accomplish, you might have better luck with a DockPanel or a Grid. Hard to tell without knowing more about your layout.
Edit: Okay, it looks like the problem is indeed the
HeaderedContentControl
parent -- but not directly. HeaderedContentControl isn't a panel, so it doesn't do any layout of its own (and its descendant, GroupBox, doesn't have this same problem). The problem is its default template -- which includes a StackPanel. The good news is, you're free to use a different template, let's say one with a DockPanel instead:If you leave off the
<HeaderedContentControl.Style>
part, this reproduces your problem; but with the style in place, it allows the GroupBox to fill its container, so the ScrollViewer will get a scrollbar when you want it to.如果前面的答案不能解决问题,您还可以尝试将网格的宽度、高度绑定到父级用户控件的实际宽度、实际高度。类似于:
在这种情况下,您没有设置显式的宽度和高度,而是将网格宽度/高度限制为其所在的 UserControl 的约束。
If the previous answer doesn't fix the problem, you could also try binding the Width, Height of your grid to the ActualWidth, ActualHeight of your parent UserControl. Something like:
In this case you aren't setting an explicit width and height but you are limiting the Grids width/height to the constraints of the UserControl it sits in.
我遇到了同样的问题,在阅读此回复后,我用 UserControl 中的网格替换了所有 StackPanels。它解决了滚动条问题。
I had the same issue, after reading this response I replaced all StackPanels with Grids in UserControl. It resolved the Scrollbar issue.
尝试完全删除网格并直接在 GroupBox 上设置 HorizontalAlignment 和 VerticalAlignment。如果布局面板只有一个子项,那么它通常是多余的......这在您的情况下可能是正确的。
如果这不起作用......你的网格控件的父级是什么?
Try removing the grid entirely and setting the HorizontalAlignment and VerticalAlignment directly on the GroupBox. If a layoutpanel has only one child, it's often redundant... this migth be true in your case.
If that doesn't work... what's the parent of your grid control?
为什么不只使用列表框而不是项目控件,它有一个内置的滚动查看器。
Why not just use a listbox instead of an itemscontrol, that has a built in scrollviewer.
他们是不同的。如果您不想选择项目,则不要使用列表框。它会更重,并且每次用户单击条目时也会取消选择。只需将 ItemsControl 放入 ScrollViewer 中即可
They are different. If you do not want to have the items selectable, then don't use a ListBox. It is going to be heavier, and will also have the deselect a selection everytime the user clicks on an entry. Just put the ItemsControl in a ScrollViewer
我对 ListBox 遇到了同样的问题,它没有扩展,并且滚动查看器没有出现。我解决如下:
I had the same problema with ListBox, it wasn't expanding and the scroll viewer didn't appear. I solved it as follows: