WPF 中基于位置的样式可能吗?
在 CSS 中,可以根据 HTML 元素在元素树中的位置来设置它们的样式:
div#container div#menu a
我想在 WPF 中执行相同的操作,以便仅对菜单部分中的链接进行样式设置。
- 问题#1:无论如何,上述内容在 WPF 中也可能吗?
我正在考虑嵌套在 StatusBar 中的分隔符的以下内容:
<Style TargetType="{x:Type StatusBar}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Padding" Value="0,0,20,0" />
<Style.Resources>
<Style TargetType="{x:Type Separator}">
<Setter Property="Width" Value="20" />
<Setter Property="Background" Value="Green" />
</Style>
</Style.Resources>
</Style>
这部分 XAML 包含在资源字典中。 StatusBar 显示为透明背景和正确的填充。但是,不幸的是,绿色分隔符不显示。它只显示默认的灰色 1px 宽条。
- 问题#2:如果这是正确的解决方案,有人知道为什么这不起作用吗?
提前致谢。
In CSS it is possible to style HTML elements based on their location in the element tree:
div#container div#menu a
I'd like to do the same in WPF, so that only links in a menu section are styled.
- Question #1: One way or the other, is the above also possible in WPF?
I was thinking about the following for Separators nested in a StatusBar:
<Style TargetType="{x:Type StatusBar}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Padding" Value="0,0,20,0" />
<Style.Resources>
<Style TargetType="{x:Type Separator}">
<Setter Property="Width" Value="20" />
<Setter Property="Background" Value="Green" />
</Style>
</Style.Resources>
</Style>
This piece of XAML is included in a resource dictionary. The StatusBar shows up with a transparent background and correct padding. However, the green separator unfortunately doesn't display. It just shows the default gray 1px wide bar.
- Question #2: If this is the correct solution, anyone knows why this doesn't work?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己偶然发现了一个答案。我提到的代码对于每种情况都是正确的,除了分隔符之外。 Separator 元素应使用特定键进行样式设置,详细描述如下: http://devlicio.us/blogs/christopher_bennage/archive/2008/06/19/styling-separators-in-wpf.aspx。
Stumbled upon an answer myself. The code I mentioned is correct for every case, except the Separator. The Separator element should be styled with a specific key, detailed described here: http://devlicio.us/blogs/christopher_bennage/archive/2008/06/19/styling-separators-in-wpf.aspx.
我不久前写了一篇博客文章,其中我采用了 CSS 选择器引擎 (Fizzler) 并将其应用到 WPF:
http://www.scottlogic.co.uk/blog/colin/2009/03/using-css-selectors-for-styling-in-wpf/
它允许您使用 CSS选择器来定位元素并向它们应用样式。它还会合并样式,以便如果多个 CSS 选择器匹配,则每个选择器的样式都会合并在一起。
请参见以下示例:
I wrote a blog post a while back where I took a CSS selector engine (Fizzler) and applied it to WPF:
http://www.scottlogic.co.uk/blog/colin/2009/03/using-css-selectors-for-styling-in-wpf/
It allows you to use CSS selectors to target elements and apply styles to them. It also merges styles so that if multiple CSS selectors match, the styles from each are merged together.
See the following example: