A style that activates depending on another style?

I am trying to create a style that applies only if the parent of the element to which the style belongs has a different special style. Like CSS, where you can do ".class1.class2" to indicate that the theme "class2" only applies if it is inside an element with class "class1".

I do not want to use any external libraries or libraries for this task. I want to know if I can implement it myself.

I tried using MultiTriggers with no luck.

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

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

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

The conditions that I tried to write to do this work are as follows: the font size condition is true, and the other is not. Conditions are inside a style that applies to all text blocks 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 the ListBoxItem style for the Suggestion style. PinnedSuggestion looks exactly the same (with the exception of 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>

Contentpresenter ListBoxItem - , , , .

, ...

ListBoxItem "" TextBlock. TextBlock (- ) , , , .

. , .

+3
1

, , 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>

+2

Source: https://habr.com/ru/post/1782040/


All Articles