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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:05:48+00:00 2026-05-19T16:05:48+00:00

Everything is in the title: I’ve read more than once that setting a style

  • 0

Everything is in the title:

I’ve read more than once that setting a style like this:

<Style TargetType="TextBox">...</Style>

was roughly equivalent to:

<Style x:Key="{x:Type TextBox}" TargetType="TextBox">...</Style>

(last time in a comment on another question)

both should apply the style to all textBoxes in the app (if they are put in the app’s resources of course)

but I tried both in my apps, and only the second one with the x:Key defined works.

it seams quite logical for me, since the first one does not know where to be applied without any x:Key property set, but then what is the point of the first syntax?

Edit: example of code in my app that works fine:

<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
     <Setter Property="HorizontalContentAlignment" Value="Left"/>
     <Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>

and code that doesn’t:

<Style TargetType="{x:Type ComboBoxItem}">
     <Setter Property="HorizontalContentAlignment" Value="Left"/>
     <Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>

I wrote this to get rid of the binding issues you get with comboBoxItems when you manipulate an existing ComboBox’s itemsSource. And the first code works fine, but the second one does not.

you can see this easily by setting the horizontalContentAlignment to Right

Edit 2: This code is simply put in a resource dictionary in my App.xaml. And replacing TargetType="{x:Type ComboBoxItem}" with TargetType="ComboBoxItem" makes no difference whatsoever

Edit 3: I just realized I might have forgotten to precise something important (sorry about that): though the styles are defined in xaml, I actually add the controls to the layout in my code behind as they are added dynamically. Might be where the trouble lies…

  • 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-19T16:05:49+00:00Added an answer on May 19, 2026 at 4:05 pm

    As shown in the first example above,
    setting the TargetType property to
    TextBlock without assigning the style
    with an x:Key allows your style to be
    applied to all TextBlock elements.
    What actually happens is that doing so
    implicitly sets the x:Key to {x:Type
    TextBlock}. This also means that if
    you give the Style an x:Key value of
    anything other than {x:Type
    TextBlock}, the Style would not be
    applied to all TextBlock elements
    automatically. Instead, you need to
    apply the style to the TextBlock
    elements explicitly.

    Considering that this is from the official documentation, your issue has to be an anomaly. I have seen a few such oddities and they are not all too unexpected since the coding behind WPF is bound to be imperfect.

    (Is there a difference in outcomes between TargetType="ComboBoxItem" and TargetType="{x:Type ComboBoxItem}" if the key is omitted?)

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

Sidebar

Related Questions

Everything is in the title, I doesn't understand what the statement below does: #import
Everything in on the title, is there a way to extract the list of
The title pretty much says it all. The precise error message that seems to
After trying everything I could, I finally created this test table: CREATE TABLE test_table
Like in the title, I want to encrypt data, which is sent over network.
I've tried everything Google and StackOverflow have recommended (that I could find) including using
Everything works just fine, but when I remove {queue:false, duration: 800, easing: 'easeInOutQuart'} from
I know the title is really unclear, so give me a moment :) I'm
ok i got everything working. the texture only applys for two sides only of
I'm using the AtomFeedHelper and everything is working fine except for one feed where

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.