在 StackPanel 中设置项目之间的间距的简单方法是什么?
有没有一种简单的方法可以设置 StackPanel 内项目之间的默认空间,这样我就不必在每个项目上设置 Margin 属性?
Is there an easy way to set default space between items inside StackPanel so I'll don't have to set Margin property on each item?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我使用透明分隔符,效果很好:
您当然可以使用边距,但如果您想更改边距,则必须更新所有元素。
分隔符甚至可以在静态资源中设置样式。
附属财产也可以做到这一点,但我认为这是矫枉过正。
I use a transparent separator, which works well:
You can of course use margins but then if you want to change the margins you have to update all of the elements.
The separator can even be styled in a static resource.
An attached property could do it too but I think it's overkill.
如果所有控件都相同,则按照 IanR 建议执行并实现捕获该控件的样式。如果不是,那么您无法为基类创建默认样式,因为它不起作用。
对于此类情况,最好的方法是使用一个非常巧妙的技巧 - 附加属性(又名 WPF4 中的行为),
您可以创建一个具有附加属性的类,如下所示:
现在,要使用它,您需要做的就是将此附加属性附加到您想要的任何面板,如下所示:
当然完全可重用。
if all the controls are the same then do as IanR suggested and implement a Style that catches that control. if it's not then you can't create a default style to a base class because it just won't work.
the best way for situations like these is to use a very neat trick - attached properties (aka Behaviors in WPF4)
you can create a class that has an attached property, like so:
now, to use it, all you need to do is to attach this attached property to any panel you want, like so:
Completely reusable of course.
我发现在堆栈面板内创建一个网格,然后添加所需的列数(或行数),如下所示:
I find that creating a grid inside the stack panel, then adding the desired number of columns (or rows) as follows:
接受的答案不再有效。但我使用该答案和该答案的作者(Elad Katz)的博客来制作一个工作代码(在 .Net Core 中测试),我在此处复制:
然后您使用:
The accepted answer doesn't work anymore. But I used that answer and the blog the author of that answer (Elad Katz) to make a working code (tested in .Net Core) that I reproduce here:
Then you use: