一种风格依赖于另一种风格而激活?

发布于 2024-10-09 06:50:05 字数 2394 浏览 0 评论 0原文

我正在尝试创建一种样式,仅当该样式引用的元素的父元素具有其他特定样式时才应用该样式。有点像在 CSS 中,您可以执行“.class1 .class2”来指定“class2”主题仅在其位于类为“class1”的元素内时才适用。

我不希望使用任何形式的外部 DLL 或库来完成此任务。我想知道是否可以自己实现。

我尝试过使用 MultiTriggers 但没有成功。

我有一个适用于所有 TextBlock 的样式。我希望文本块执行以下操作:

如果文本块的字体大小为 11 并且父元素的样式为“PinnedSuggestion”,请将前景色设置为“#FF505050”。< /em>

如果文本块的字体大小为 11,且父元素的样式为“建议”,则将前景色设置为“#FFCCCCCC”。

我尝试编写以使这项工作正常进行,如下(字体大小条件为真,但另一个条件不是)。这些条件位于一般适用于所有文本块的样式内。

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FontSize}" Value="11" />
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Style}" Value="{StaticResource PinnedSuggestion}" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Foreground" Value="#FFFF5050"></Setter>
                </MultiDataTrigger>

我不确定在这种情况下我做错了什么。下面您可以看到我的“建议”样式的 ListBoxItem 样式。 PinnedSuggestion 看起来完全一样(除了一些细微的变化)。

<Style x:Key="Suggestion" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Grid Name="Container" Margin="0,0,0,0">
                    <Rectangle Margin="0,2,0,2" Stroke="Black" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                    <Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3"  SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                    <ContentPresenter Margin="0"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

该 ListBoxItem 样式的 contentpresenter 包含我希望此技术使用的文本块。

所以,总结一下......

应用了“Suggestion”样式的 ListBoxItem 将在其中包含一个 TextBlock。 TextBlock 样式(由于其目标类型)将自动应用到该样式,并且我希望上述多重触发条件能够正常工作。

我的情况有点难以解释。我尽我所能地解释了一切。

I am trying to make a style that only gets applied if the parent element of the element that the style refers to, has another specific style. Kind of like in CSS where you can do ".class1 .class2" to specify that the "class2" theme only applies if it is within an element with the class "class1".

I do not wish to use any form of external DLLs or libraries for this task. I want to know if it's possible to implement on my own.

I've tried using MultiTriggers with no luck.

I have a style that applies to all TextBlocks. I want the textblock to do the following:

If the font-size of the textblock is 11 and the parent element's style is "PinnedSuggestion", set the foreground color to "#FF505050".

If the font-size of the textblock is 11 and the parent element's style is "Suggestion", set the foreground color to "#FFCCCCCC".

The conditions that I have tried to write to make this work, are as follows (the font-size condition is true, but the other one is not). The conditions are inside a style that applies to all textblocks in general.

                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FontSize}" Value="11" />
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Style}" Value="{StaticResource PinnedSuggestion}" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Foreground" Value="#FFFF5050"></Setter>
                </MultiDataTrigger>

I am not sure what I am doing wrong in this case. Below you see my ListBoxItem style for the "Suggestion" style. The PinnedSuggestion looks exactly the same (except for a few minor changes).

<Style x:Key="Suggestion" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Grid Name="Container" Margin="0,0,0,0">
                    <Rectangle Margin="0,2,0,2" Stroke="Black" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                    <Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3"  SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                    <ContentPresenter Margin="0"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The contentpresenter of that ListBoxItem style is what contains the textblocks that I want this technique to work with.

So, to summarize...

A ListBoxItem with the style "Suggestion" applied will have a TextBlock in it. The TextBlock style (due to its target type) will automatically apply to that, and I want the multitrigger conditions described above to work as they should.

My situation is kind of hard to explain. I explained everything as well as I could.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

╄→承喏 2024-10-16 06:50:05

您要检查其样式的父元素不是 TextBlock 的直接父元素;它可以在视觉树中任意更高。因此,您的第二个条件需要查找特定类型的祖先,如下所示:

<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=Style}" Value="{StaticResource Suggestion}" />

这是一个完整的工作示例,使用 .NET4 进行了测试:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="Suggestion" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Name="Container" Margin="0,0,0,0">
                        <Rectangle Margin="0,2,0,2" Stroke="Blue" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3"  SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <ContentPresenter Margin="0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="PinnedSuggestion" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Name="Container" Margin="0,0,0,0">
                        <Rectangle Margin="0,2,0,2" Stroke="Green" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3"  SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <ContentPresenter Margin="0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="Neutral" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Name="Container" Margin="0,0,0,0">
                        <Rectangle Margin="0,2,0,2" Stroke="Black" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3"  SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <ContentPresenter Margin="0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FontSize}" Value="11" />
                    <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=Style}" Value="{StaticResource Suggestion}" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Foreground" Value="Red"></Setter>
            </MultiDataTrigger>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FontSize}" Value="11" />
                    <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=Style}" Value="{StaticResource PinnedSuggestion}" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Foreground" Value="Yellow"></Setter>
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <ListBox>
        <ListBoxItem Style="{StaticResource Neutral}">
            <TextBlock FontSize="10" Text="Style=Neutral, FontSize=10"/>
        </ListBoxItem>
        <ListBoxItem Style="{StaticResource Neutral}">
            <TextBlock FontSize="11" Text="Style=Neutral, FontSize=11"/>
        </ListBoxItem>
        <ListBoxItem Style="{StaticResource Suggestion}">
            <TextBlock FontSize="10" Text="Style=Suggestion, FontSize=10"/>
        </ListBoxItem>
        <ListBoxItem Style="{StaticResource Suggestion}">
            <TextBlock FontSize="11" Text="Style=Suggestion, FontSize=11"/>
        </ListBoxItem>
        <ListBoxItem Style="{StaticResource PinnedSuggestion}">
            <TextBlock FontSize="10" Text="Style=PinnedSuggestion, FontSize=10"/>
        </ListBoxItem>
        <ListBoxItem Style="{StaticResource PinnedSuggestion}">
            <TextBlock FontSize="11" Text="Style=PinnedSuggestion, FontSize=11"/>
        </ListBoxItem>
    </ListBox>
</Grid>

The parent element whose style you want to inspect is not the direct parent of the TextBlock; it could be arbitrarily higher in the visual tree. So your second condition needs to look for an ancestor of a particular type like this:

<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=Style}" Value="{StaticResource Suggestion}" />

Here is a complete working example, tested with .NET4:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="Suggestion" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Name="Container" Margin="0,0,0,0">
                        <Rectangle Margin="0,2,0,2" Stroke="Blue" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3"  SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <ContentPresenter Margin="0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="PinnedSuggestion" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Name="Container" Margin="0,0,0,0">
                        <Rectangle Margin="0,2,0,2" Stroke="Green" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3"  SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <ContentPresenter Margin="0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="Neutral" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Name="Container" Margin="0,0,0,0">
                        <Rectangle Margin="0,2,0,2" Stroke="Black" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3"  SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Rectangle>
                        <ContentPresenter Margin="0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FontSize}" Value="11" />
                    <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=Style}" Value="{StaticResource Suggestion}" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Foreground" Value="Red"></Setter>
            </MultiDataTrigger>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FontSize}" Value="11" />
                    <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=Style}" Value="{StaticResource PinnedSuggestion}" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Foreground" Value="Yellow"></Setter>
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <ListBox>
        <ListBoxItem Style="{StaticResource Neutral}">
            <TextBlock FontSize="10" Text="Style=Neutral, FontSize=10"/>
        </ListBoxItem>
        <ListBoxItem Style="{StaticResource Neutral}">
            <TextBlock FontSize="11" Text="Style=Neutral, FontSize=11"/>
        </ListBoxItem>
        <ListBoxItem Style="{StaticResource Suggestion}">
            <TextBlock FontSize="10" Text="Style=Suggestion, FontSize=10"/>
        </ListBoxItem>
        <ListBoxItem Style="{StaticResource Suggestion}">
            <TextBlock FontSize="11" Text="Style=Suggestion, FontSize=11"/>
        </ListBoxItem>
        <ListBoxItem Style="{StaticResource PinnedSuggestion}">
            <TextBlock FontSize="10" Text="Style=PinnedSuggestion, FontSize=10"/>
        </ListBoxItem>
        <ListBoxItem Style="{StaticResource PinnedSuggestion}">
            <TextBlock FontSize="11" Text="Style=PinnedSuggestion, FontSize=11"/>
        </ListBoxItem>
    </ListBox>
</Grid>

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