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

I hope that the title and this simple example says everything. public partial class
I think the title explains everything; is this possible? Develop a ClassLibrary project to
Everything is in the title. On the official documentations it is stated that Note
More or less everything is in the title, I am looking for a way
I guess everything is in the title... I know that Dictionary<TKey,TValue> does keep the
I have a simple view and a helper that defines the title. Everything works
Everything is in the title, I want to be able to delete account that
Almost everything is in the title : Here's what I'd like to do :
Well the title explains pretty much everything. I want to use something like p
like in title:) I wrote from tutorial: http://msdn.microsoft.com/en-us/wp7trainingcourse_yourfirstwp7applab_topic3 does everything like there. my code:

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.