WPF 中基于位置的样式可能吗?

发布于 2024-11-23 16:15:33 字数 879 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见终念 2024-11-30 16:15:33

我自己偶然发现了一个答案。我提到的代码对于每种情况都是正确的,除了分隔符之外。 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.

天冷不及心凉 2024-11-30 16:15:33

我不久前写了一篇博客文章,其中我采用了 CSS 选择器引擎 (Fizzler) 并将其应用到 WPF:

http://www.scottlogic.co.uk/blog/colin/2009/03/using-css-selectors-for-styling-in-wpf/

它允许您使用 CSS选择器来定位元素并向它们应用样式。它还会合并样式,以便如果多个 CSS 选择器匹配,则每个选择器的样式都会合并在一起。

请参见以下示例:

<css:StyleSheet x:Key="cssStyles">
    <css:StyleSheet.Rules>

        <css:StyleRule Selector=".form Grid *" SelectorType="LogicalTree">
            <css:StyleRule.DeclarationBlock>
                <css:StyleDeclarationBlock>
                    <css:StyleDeclaration Property="Margin" Value="4,4,4,4"/>
                </css:StyleDeclarationBlock>
            </css:StyleRule.DeclarationBlock>
        </css:StyleRule>

        <css:StyleRule Selector=".form TextBlock.mandatory">
            <css:StyleRule.DeclarationBlock>
                <css:StyleDeclarationBlock>
                    <css:StyleDeclaration Property="Foreground" Value="Red"/>
                </css:StyleDeclarationBlock>
            </css:StyleRule.DeclarationBlock>
        </css:StyleRule>

        <css:StyleRule Selector="Border.form">
            <css:StyleRule.DeclarationBlock>
                <css:StyleDeclarationBlock>
                    <css:StyleDeclaration Property="BorderThickness" Value="2"/>
                    <css:StyleDeclaration Property="BorderBrush" Value="Black"/>
                    <css:StyleDeclaration Property="CornerRadius" Value="5"/>
                    <css:StyleDeclaration Property="Margin" Value="10,10,10,10"/>
                </css:StyleDeclarationBlock>
            </css:StyleRule.DeclarationBlock>
        </css:StyleRule>

        <css:StyleRule Selector=".form .title">
            <css:StyleRule.DeclarationBlock>
                <css:StyleDeclarationBlock>
                    <css:StyleDeclaration Property="HorizontalAlignment" Value="Stretch"/>
                    <css:StyleDeclaration Property="HorizontalContentAlignment" Value="Center"/>
                    <css:StyleDeclaration Property="Background" Value="DarkBlue"/>
                    <css:StyleDeclaration Property="Foreground" Value="White"/>
                    <css:StyleDeclaration Property="FontSize" Value="13"/>
                    <css:StyleDeclaration Property="Padding" Value="3,3,3,3"/>
                    <css:StyleDeclaration Property="FontWeight" Value="Bold"/>
                </css:StyleDeclarationBlock>
            </css:StyleRule.DeclarationBlock>
        </css:StyleRule>

    </css:StyleSheet.Rules>
</css:StyleSheet>

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:

<css:StyleSheet x:Key="cssStyles">
    <css:StyleSheet.Rules>

        <css:StyleRule Selector=".form Grid *" SelectorType="LogicalTree">
            <css:StyleRule.DeclarationBlock>
                <css:StyleDeclarationBlock>
                    <css:StyleDeclaration Property="Margin" Value="4,4,4,4"/>
                </css:StyleDeclarationBlock>
            </css:StyleRule.DeclarationBlock>
        </css:StyleRule>

        <css:StyleRule Selector=".form TextBlock.mandatory">
            <css:StyleRule.DeclarationBlock>
                <css:StyleDeclarationBlock>
                    <css:StyleDeclaration Property="Foreground" Value="Red"/>
                </css:StyleDeclarationBlock>
            </css:StyleRule.DeclarationBlock>
        </css:StyleRule>

        <css:StyleRule Selector="Border.form">
            <css:StyleRule.DeclarationBlock>
                <css:StyleDeclarationBlock>
                    <css:StyleDeclaration Property="BorderThickness" Value="2"/>
                    <css:StyleDeclaration Property="BorderBrush" Value="Black"/>
                    <css:StyleDeclaration Property="CornerRadius" Value="5"/>
                    <css:StyleDeclaration Property="Margin" Value="10,10,10,10"/>
                </css:StyleDeclarationBlock>
            </css:StyleRule.DeclarationBlock>
        </css:StyleRule>

        <css:StyleRule Selector=".form .title">
            <css:StyleRule.DeclarationBlock>
                <css:StyleDeclarationBlock>
                    <css:StyleDeclaration Property="HorizontalAlignment" Value="Stretch"/>
                    <css:StyleDeclaration Property="HorizontalContentAlignment" Value="Center"/>
                    <css:StyleDeclaration Property="Background" Value="DarkBlue"/>
                    <css:StyleDeclaration Property="Foreground" Value="White"/>
                    <css:StyleDeclaration Property="FontSize" Value="13"/>
                    <css:StyleDeclaration Property="Padding" Value="3,3,3,3"/>
                    <css:StyleDeclaration Property="FontWeight" Value="Bold"/>
                </css:StyleDeclarationBlock>
            </css:StyleRule.DeclarationBlock>
        </css:StyleRule>

    </css:StyleSheet.Rules>
</css:StyleSheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文