peter
peter

Reputation: 2103

Style changes completely

How come that this ModernUI TextBox

enter image description here

changes its appearance to this

enter image description here

when all that I do is add this style:

<UserControl.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="TextAlignment" Value="Right"/>
    </Style>
</UserControl.Resources>

When I set the property directly in the element:

<TextBox Text="" TextAlignment="Right"></TextBox>

Everything looks as it should:

enter image description here

I really don't understand that and would be thankful for a short hint.

Upvotes: 3

Views: 72

Answers (3)

David Sherret
David Sherret

Reputation: 106580

The issue is that it will completely replace the other style.

Add a BasedOn attribute to specify that this style will extend the ModernUI style:

<UserControl.Resources>
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource whateverModernUIStyleNameIs}">
        <Setter Property="TextAlignment" Value="Right"/>
    </Style>
</UserControl.Resources>

You can also specify the implict style: BasedOn="{StaticResource {x:Type TextBox}}", as pointed out by Mike C.

Read this question for more details: How to apply multiple styles in WPF

Upvotes: 6

Frank J
Frank J

Reputation: 1696

Because you replaced the existing style with one that only sets the text alignment.

Create a style in your Resources based on the default style

<Style
    BasedOn="{StaticResource {x:Type TextBox}}"
    TargetType="{x:Type TextBox}">
    <Setter Property="TextAlignment" Value="Right"/>
</Style>

Upvotes: 3

Chris W.
Chris W.

Reputation: 23270

By specifying that you're setting a new style template of TargetType of TextBox you're implicitly overriding whatever other style template you have set for it already.

If you add BasedOn to your template to reference the other style giving it the red border it will inherit everything else as it should and only change the property you have your setter for.

So <Style TargetType="{x:Type TextBox}"> then becomes;

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource TheKeyNameOfTheOtherStyleTemplateYouAreReferencing}">

Make sense? Except why would you need to place a style setter for it, when you already have a property bound to the template to do what you want with one dependency property?

Upvotes: 4

Related Questions