如何使 WPF Expander 伸展?
WPF 中的 Expander
控件不会拉伸以填充所有可用空间。 XAML中有没有解决方案?
The Expander
control in WPF does not stretch to fill all the available space. Is there any solutions in XAML for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您需要做的就是:
http: //joshsmithonwpf.wordpress.com/2007/02/24/stretching-content-in-an-expander-header/
All you need to do is this:
http://joshsmithonwpf.wordpress.com/2007/02/24/stretching-content-in-an-expander-header/
不可拉伸的
Expander
通常是不可拉伸的父控件的问题。也许其中一个父控件定义了HorizontalAlignment
或VerticalAlignment
属性?如果您可以发布一些示例代码,我们可以给您更好的答案。
Non stretchable
Expander
s is usually the problem of non stretchable parent controls.. Perhaps one of the parent controls has defined aHorizontalAlignment
orVerticalAlignment
property?If you can post some sample code, we can give you a better answer.
此解决方案要简单得多,并且不会影响您可能在应用程序中使用的其他扩展器控件。
This solution is a lot simpler and does not effect other expander controls you may using in your application.
接受的答案绘制在控件之外,因为标题内容位于一列中,而扩展按钮位于第一列中。 对于某些情况可能已经足够了。
如果您想要一个干净的解决方案,您必须更改扩展器的模板。
另一种方法是附加属性:
它可以让你做:
请注意,附加属性的使用有些脆弱,因为它假设模板中有一个
ContentPresenter
。The accepted answer draws outside the control because the header content is in one column and the expander button is in the first. Might be good enough for some cases.
If you want a clean solution you have to change the template of the expander.
Another way is an attached property:
It lets you do:
Note that the use of the attached property is somewhat fragile as it assumes that there is a
ContentPresenter
in the template.我很惊讶没有人提到只编辑控件模板,所有其他解决方案都有一些非常严重的缺点,例如它没有正确计算宽度,没有重绘,渲染时间等...
此控件模板将修改标题的 contentpresenter 水平对齐方式与实际的扩展器匹配(意味着它会拉伸)
I am pretty surprised nobody has mentioned to just edit the control template, all the other solutions have some pretty serious drawbacks weather its not calculating width properly, not redrawing, render time etc...
This control template will modify the header's contentpresenter horizontal alignment to match the actual expanders (meaning it will stretch)
我同意 HTH - 检查您将 Expander 放入哪种容器中...StackPanel 始终会将其子项折叠到它们可以达到的最小尺寸。
我在我的项目中经常使用扩展器,如果将它们放入 Grid / DockPanel 中,那么扩展器将填充所有可用空间(假设它的垂直和水平方向设置为拉伸)。
乔纳森关于将 Expander 的宽度绑定到容器的宽度的建议可能会有点棘手......几周前我尝试了这种技术,发现它在某些情况下会产生不良结果,因为它会抑制布局系统的功能。
PS:作为一般提示(我确信我会因为写这篇文章而受到批评),如果您不确定控件的布局容器类型,那么从网格开始。 使用列和 行定义允许您非常轻松地控制子控件是否使用最小空间(“自动”)、最大空间(“*”)或精确的空间量(“[数字]”)。
I Agree with HTH - check what sort of a container you're putting the Expander in... the StackPanel will always fold it's children down to the smallest size they can go to.
I'm using Expanders a lot in my project, and if you drop them into a Grid / DockPanel, then the expander will fill all available space (assuming it's Vertical & Horizontal orientations are set to Stretch).
Jonathan's suggestion of Binding the Expander's width to the container's width can get a bit tricky... I tried this technique a few weeks back and found that it can producte undesirable results in some cases, because it can inhibit the functioning of the layout system.
PS: As a general tip (and I'm sure I'm gonna get flamed for writing this), if you're unsure of what sort of layout-container to your controls in, then start off with a Grid. Using the Column & Row definitions allows you to very easily control whether child controls use minimum space ("Auto"), maximum space ("*") or an exact amount of space ("[number]").
Silverlight 工具包包含一个 Accordion 控件的作用就像一个扩展器,始终延伸到可用空间。 我还没有测试过它,但它可能也适用于 WPF,就像 Silverlight 图表控件一样。
The Silverlight Toolkit includes an Accordion control which acts like an expander that always stretches to the available space. I haven't tested it yet, but it might be functional for WPF too, like the Silverlight Chart controls.