Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3616244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:28:32+00:00 2026-05-18T22:28:32+00:00

I am trying to make a style that only gets applied if the parent

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:28:32+00:00Added an answer on May 18, 2026 at 10:28 pm

    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>
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a web page that only has content within the page
I am trying to make a JTable that has column spans available. Specifically, I
I am trying to make a sort of progress bar that gets filled, with
I am trying to make a menu that works like the one on this
I am in the process of trying to make an app that has most
I'm trying to make Javascript change the style of certain DIV IDs by changing
Trying to make a make generic select control that I can dynamically add elements
Trying to make a generic PL/SQL procedure to export data in specific XML format,
I'm trying to make a context menu for a control that is linked to
I am trying to make a div, that when you click it turns into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.