如何使 WPF 窗口扩展以适应动态变化的 ItemsControl 的内容?
我有一个 WPF 应用程序,它以一个非常小的窗口开始。面积为 200x100 的东西。当从外部源引发某些事件时,我将一个控件添加到窗口的 ItemsControl 区域。项目控件当前设置为使用 StackPanel PanelTemplate。
我想要的是,当控件添加到 itemscontrol 时,应用程序窗口会增长。这有可能吗?其背后的想法是始终保持窗口尽可能小。当从 itemscontrol 中删除控件时,它也应该缩小。
谢谢。
I've got a WPF application which starts off with a very small window. Something in the area of 200x100. When certain events are raised from an external source, I add a control to the ItemsControl area of my window. The items control is currently set to use a StackPanel PanelTemplate.
What I want, is for the application window to grow when controls are added to the itemscontrol. Is this at all possible? The idea behind this is to keep the window as small as possible at all times. It should also shrink when controls are removed from the itemscontrol.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的。您可以使用
Window.SizeToContent
属性来做到这一点。只需在Window
或内部的ItemsControl
上设置MaxHeight
和MaxWidth
即可,这样它就不会继续增长超出屏幕范围。Yes, it is possible. You can use
Window.SizeToContent
property to do that. And just set aMaxHeight
andMaxWidth
onWindow
or theItemsControl
inside just so it won't keep growing and getting out of screen bounds.如果您想在 WPF 的 XAML 中设置它,您可以使用
它,如果需要,可以单独设置
Height
和Width
。And if you would want to set it in XAML for WPF you could use
It can be separately set for the
Height
and theWidth
if required.