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

  • SEARCH
  • Home
  • 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 8878439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:42:42+00:00 2026-06-14T19:42:42+00:00

I have several elements and all of them should look the same if X.

  • 0

I have several elements and all of them should look the same if “X”. The problem is, “X” is different for each element, so I can’t easily use a shared style for them. But in all cases, the setters are the same, so I would like to have them all in one place. How can I achieve that?

More concretely, I have this code:

<TextBlock Name="text1">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding SelectedMonth, Converter={conv:IsNullConverter}}"
                             Value="False">
                    <Setter Property="Background" Value="RoyalBlue" />
                    <!-- possibly other setters -->
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
<TextBlock Name="text2">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                    <Setter Property="Background" Value="RoyalBlue" />
                    <!-- possibly other setters, same ones as above -->
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

Here, I would like to put the two Setters to some common place, so that I wouldn’t have to always change them one by one. Is there some way to do that?

I tried putting a collection of Setter together and then using something like DataTrigger.Setters="{StaticResource SharedSetters}". This doesn’t work, but if it did, it would be exactly what I want.

  • 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-06-14T19:42:43+00:00Added an answer on June 14, 2026 at 7:42 pm

    First of all, you could have a common style which would be the base (BasedOn attribute) for all the other styles. This style would contain a normal Trigger which is controlled by a custom boolean attached property, and the setters you want to reuse. Then, in the other styles, which are based on the base style, your DataTriggers would only set the attached property, and let the base style set all the other properties.

    Assuming you have created the attached property IsSelected on the type Foo, the base style would look something like:

    <Style x:Key="IsSelectedStyle" TargetType="TextBlock">
        <Style.Triggers>
            <Trigger Property="local:Foo.IsSelected" Value="True">
                <Setter Property="Background" Value="RoyalBlue" />
                <!-- possibly other setters -->
            </Trigger>
        </Style.Triggers>
    </Style>
    

    And then you would use it like this:

    <TextBlock>
        <TextBlock.Style>
            <Style TargetType="TextBlock" BasedOn="{StaticResource IsSelectedStyle}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSelected}" Value="True">
                        <Setter Property="local:Foo.IsSelected" Value="True" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jquery data selector I have several elements with class 'connection_name'. Each one
I have several HTML elements (buttons) that fire the same JQuery AJAX request. When
I've got several elements on a HTML page which have the same class -
I have an application that contains several transitioning elements. These same elements react to
I wonder if it's possible to have several div elements with different id that
I have several elements that are needed in each view and trying to set
I have several elements on the html page, I call them through javascript like
I have a TabControl and under it I have several elements like TreeView and
I have several div elements for charts. Yslow is saying to reduce DOM elements.
I have created several UI elements in the viewDidLoad method. I want to change

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.