使用滚动条自动调整wpf元素大小(富文本框、列表框)vb
我遇到一个问题,我有列表框和富文本框等元素,我想根据窗口的大小在 xaml 中自动设置其大小,但我只想将其调整为窗口的大小,然后如果内容大于此值,请放置滚动条。
不幸的是,我可以让滚动条工作的唯一方法是设置列表框/富文本框的特定高度(这不起作用,因为我希望它自动调整大小到它所包含的网格的高度, 是窗口的高度(自动)。
这通常
I'm having a problem where I have elements such as Listboxes and Rich Text boxes that I want to set to size automatically in xaml according to the size of the window, but I only want it to resize to the size of the window and then put scrollbars if the content is any bigger than that.
Unfortunately, the only way I can get scroll bars to work is if I set a specific height of the listbox/rich text box (which does not work because I want it to automatically resize to the height of the grid that it is contained within, which is generally the height of the window (auto).
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要为
Width
和Height
使用固定值 - 您应该使用MinWidth
和MinHeight
属性。然后尝试与此类似的布局:如果将对齐属性设置为 Stretch 并且至少将一行/列设置为,则
Grid
通常会使用它获得的所有空间星星大小。在这种情况下,只有一行和一列,隐式创建,默认都是星形大小。要使
ScrollViewer
工作,您需要以某种方式设置内容控件的最小大小,否则ScrollViewer
不知道何时激活ScrollBar
。在上面的示例中,我使用ListBox
的MinHeight
和MinWidth
属性来完成此操作,但您也可以在ListBox
上设置这些属性。 code>Grid 的RowDefinition
和/或ColumnDefinition
。现在,如果您调整窗口大小,使
Width
变得小于500
,您将看到滚动条出现。看看吧。You do not need to use fixed values for
Width
andHeight
- you should rather specify a minimum width/height for your controls using theMinWidth
andMinHeight
properties. Then try a layout similar to this:The
Grid
generally uses all the space it gets if its alignment properties are set toStretch
and if at least one row/column is set to be star-sized. In this case, there are only one row and one column, implicitly created, both star-sized by default.To make the
ScrollViewer
work, you need to somehow set a minimum size of your content controls because otherwise theScrollViewer
does not know when to activate theScrollBar
s. In the example above, I have done that using theMinHeight
andMinWidth
properties of theListBox
, but you could also set these properties on theGrid
'sRowDefinition
s and/orColumnDefinition
s.Now, if you resize the window, so that the
Width
becomes smaller than500
, you will see that scrollbars will appear. Just check it out.