如何将 GroupBox 标题与其内容分开设置样式?
我正在开发一个 WinForms 应用程序,在一个表单上,我使用 GroupBox 将相关控件分组在一起。我想以特定的方式设置标题的样式(例如,将标题设为粗体)。通过更改 GroupBox 的样式,我可以自定义标题,但它也会更改 GroupBox 内控件的样式。
到目前为止,还没有那么多子控件,所以我单独将它们的样式重置回默认值,但我认为必须有更好的方法......但到目前为止我还没有弄清楚。
有办法吗?
I'm developing a WinForms application and on one form, I use a GroupBox to group related controls together. I would like to style the caption in a particular way (e.g. making the caption bold). By altering the styling for the GroupBox, I can customise the caption, but it also alters the styling for controls within the GroupBox.
So far, there aren't that many child controls, so I'm individually resetting their styles back to the default, but I figure there has to be a better way... but I haven't figured it out so far.
Is there a way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是在 GroupBox 中放置另一个面板,这样您只需重置字体一次。
还有很多其他解决方案,但它们都需要更多代码。这使它保持美观和简单。
The easiest way would be to put another panel inside the GroupBox, so that you only have to reset the font once.
There are plenty of other solutions, but they all require a lot more code. This keeps it nice and simple.
Control.Font
(以及其他与样式相关的属性)的实现方式是,如果它没有显式设置,它会向其父级请求Font
。显然,这意味着您必须为所有子控件单独设置它,以确保它们不会受到父样式更改的影响。也许您应该看看所有者绘制的
GroupBox
标题?Control.Font
(and other style-related properties) is implemented in such a way that it asks its parent forFont
if it does not have explicitly set one. Obviously, this means that you would have to set it individually to all of the child controls to make sure they are not affected by changes in parent style.Maybe you should take a look at owner-drawing the
GroupBox
caption?我解决了这个问题,删除了默认组标题,并在默认组标题的同一位置在组上添加了一个粗体的新标签。
I Solved this problem removing the default group caption and add a new label witch bold over the group at same place default group caption was.