user5582011
user5582011

Reputation:

TextBox style is setting for all Textboxes

I made a style for a few specific Textboxes in my application and placed this inside of my App.xaml:

<Style x:Name="TextBoxStyle" TargetType="TextBox">
    <Setter Property="Background" Value="Blue"/>
</Style>

After doing that it set the style for every textbox in my application. I didn't even set this style on any textbox yet.

Can anyone tell me why this is happening?

Upvotes: 1

Views: 224

Answers (1)

brunnerh
brunnerh

Reputation: 184622

You need to specify x:Key (and reference using StaticResource), otherwise it's used by TargetType, hence applying to all TextBoxes.

This is a special implicit behaviour, i.e. if a Style is defined as a child of a ResourceDictionary without a key, the TargetType is used as key. There are other such implicit rules, e.g. DataTemplate creates special data template keys which then also apply the template implicitly where applicable.

Upvotes: 3

Related Questions