ScrollViewer 内的面板
我的 wpf 应用程序中遇到了一个有趣的问题。
我正在使用 mvvm,项目是在绑定到视图模型的内容控件内组成的。
例如:
<Shell>
<ScrollViewer>
<StackPanel>
<ContentControl Content="{Bidning SomeVM}"/>
<ContentControl Content="{Bidning SomeVM2}"/>
<ContentControl Content="{Bidning SomeVM3}"/>
</StackPanel>
</ScrollViewer>
</Shell>
如果堆栈面板只有一项,它可以正常工作,但由于某种原因,每个视图(解析形式 vm)需要比实际需要更多的空间(高度)。
如果需要的话,视图包含一个 GroupBox 和一个数据网格。
所以这是我的问题,有什么建议吗?
I got an interesting issue inside my wpf application.
I'm working with mvvm, and items are composed within a content control that bound to the view model.
For example:
<Shell>
<ScrollViewer>
<StackPanel>
<ContentControl Content="{Bidning SomeVM}"/>
<ContentControl Content="{Bidning SomeVM2}"/>
<ContentControl Content="{Bidning SomeVM3}"/>
</StackPanel>
</ScrollViewer>
</Shell>
If the stack panel got only one item it works fine, but for some reason each view (resolved form vm) require much more space(height) than it actually need.
The views contains a GroupBox and a datagrid inside if it's matter.
So this is my problem, any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
StackPanel 将在方向方向上以无穷大来测量其子项,默认情况下为垂直方向。因此,您的项目将以无限高度进行测量,这本质上意味着它们将根据内容调整大小 - 例如,DataGrid 将为列表中的每个项目分配、测量和排列记录(假设在控件上没有设置任何约束)。还要注意,它们不会因此而成为虚拟化,因为这依赖于使用特定约束进行测量。
请注意,即使您使用不同的面板,您也可能会遇到此行为,因为 ScrollViewer 中的 ScrollContentPresenter 会在执行滚动时测量其子级的无穷大 - 如果 CanContentScroll 设置为 false 或 ItemsPanel 未设置为 false,则会发生这种情况实现 IScrollInfo.
我不确定您想要实现什么类型的布局,因此很难提供任何具体建议。
A StackPanel is going to measure it's children with Infinity in the direction of the Orientation, which is Vertical by default. So your items will be measured with an infinite height which essentially means they're going to size to content - e.g. the DataGrid will allocate, measure and arrange a record for every item in the list (assuming no constraints were set on the control). Note too that their will be no virtualization as a result since that relies on being measured with a specific constraint.
Note, even if you used a different panel you might get this behavior as the ScrollContentPresenter within the ScrollViewer will measure it's children with infinity if it is performing the scrolling - which will happen if the CanContentScroll is set to false or if the ItemsPanel doesn't implement IScrollInfo.
I'm not sure what type of layout you're looking to acheive so it's hard to offer any specific recommendations.