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 159341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:50:30+00:00 2026-05-11T10:50:30+00:00

In <Window.Resources> I have defined following style: <Style x:Key=textBlockStyle TargetType=TextBlock> <Setter Property=Margin Value=5,0,5,0/> </Style>

  • 0

In <Window.Resources> I have defined following style:

    <Style x:Key='textBlockStyle' TargetType='TextBlock'>         <Setter Property='Margin' Value='5,0,5,0'/>     </Style> 

I have defined some grid where I have four TextBlocks:

    <WrapPanel>         <TextBlock Style='{StaticResource textBlockStyle}'>Server</TextBlock>         <TextBlock Style='{StaticResource textBlockStyle}'>IP</TextBlock>         <TextBlock Style='{StaticResource textBlockStyle}'>Port</TextBlock>         <TextBlock Style='{StaticResource textBlockStyle}'>Status</TextBlock>     </WrapPanel> 

Problem: I need to reference the textBlockStyle four times.

Question: Is it possible to set that style just once at in WrapPanel or somewhere else without repeating the reference to the style?

Maybe something like:

    <WrapPanel Style='{StaticResource textBlockStyle}'>         <TextBlock>Server</TextBlock>         <TextBlock>IP</TextBlock>         <TextBlock>Port</TextBlock>         <TextBlock>Status</TextBlock>     </WrapPanel> 

I am not searching for a global solution! I could delete that x:Key='textBlockStyle' property, but that would affect all TextBlocks in the Window. I need a more selective mechanism, but without that ugly code duplication.

  • 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. 2026-05-11T10:50:31+00:00Added an answer on May 11, 2026 at 10:50 am

    You have several options, presented here in order of how well they scale.

    Option 1: Define the Style without a key at a lower level

    You can stick the resource at the WrapPanel level so that it only affects controls inside that WrapPanel:

    <WrapPanel>     <WrapPanel.Resources>         <Style TargetType='TextBlock'>             <Setter Property='Margin' Value='5,0,5,0'/>         </Style>     </WrapPanel.Resources>      <!-- TextBlocks here --> </WrapPanel> 

    Notice the lack of key. This Style will apply to all TextBlocks within the WrapPanel.

    Option 2: Define the Style with a key and again without at a lower level

    If you define the Style at a higher level with a key, you can then define another Style at a lower level without a key, and base that Style on the higher level one:

    <Window>     <Window.Resources>         <Style TargetType='TextBlock' x:Key='textBlockStyle'>             <Setter Property='Margin' Value='5,0,5,0'/>         </Style>     </Window.Resources>      <WrapPanel>         <WrapPanel.Resources>             <Style TargetType='TextBlock' BasedOn='{StaticResource textBlockStyle'/>         </WrapPanel.Resources>          <!-- TextBlocks here -->     </WrapPanel> </Window> 

    This results in a Style being automatically applied to TextBlocks inside the WrapPanel, but not outside it. Also, you don’t duplicate the details of the Style – they are stored at a higher level.

    Option 3: Place the Styles in a ResourceDictionary and selectively merge it

    Finally, you can place your Styles in a separate ResourceDictionary and selectively merge that dictionary into a control’s Resources collection:

    <!-- TextBlockStyles.xaml --> <ResourceDictionary>     <Style TargetType='TextBlock'>         <Setter Property='Margin' Value='5,0,5,0'/>     </Style> </ResourceDictionary>  <!-- Window.xaml --> <Window>     <WrapPanel>         <WrapPanel.Resources>             <ResourceDictionary>                 <ResourceDictionary.MergedDictionaries>                     <ResourceDictionary Source='TextBlockStyles.xaml'/>                 </ResourceDictionary.MergedDictionaries>             </ResourceDictionary>         </WrapPanel.Resources>     </WrapPanel> </Window>  <!-- Alternative Window.xaml if you have only one RD to merge in --> <Window>     <WrapPanel>         <WrapPanel.Resources>             <ResourceDictionary Source='TextBlockStyles.xaml'/>         </WrapPanel.Resources>     </WrapPanel> </Window> 

    Now you can have as many style sets defined in separate dictionaries as you like, and then selectively apply them to your element tree.

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

Sidebar

Related Questions

I have the following AutoCompleteBox defined inside DataTemplate: <Window.Resources> <DataTemplate x:key=PaneTitleTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition
Within my Windows.Resources I have the following column defined: <DataGridTextColumn x:Key=CustomColumn x:Shared=False> <DataGridTextColumn.Header> <StackPanel>
I have the following window, which displays a listview. I defined a style for
In my xaml code, inside the Window.Resources section, I have defined a Data Template
I have a DataTemplate within my Window's Resources section that creates a TextBlock with
In my MainWindow.xaml, I have the following reference to a ResourceDictionary: <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries>
I have a default TextBlock style defined in App.xaml, which seems to also affect
I have a custom collection defined in my window resources as follows (in a
I have a dependency property defined in my window as below: public static readonly
I have a user-defined button, and it may have a Style property which I

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.