2
votes

I have this generic style for a custom control with this Template.

<Style TargetType="{x:Type local:MyType}">        
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MyType}">                    
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <local:MyControl />
                    </Border>
                    ...

if this control is used in a project where is this style globaly set:

<Style  TargetType="{x:Type Border}">
    <Setter Property="Padding" Value="3" />
    <Setter Property="VerticalAlignment" Value="Top" />
</Style>

But MyControl is an ordinary UserControl containing: ...

<Border .. />

Then the Border in the UserControl (residing in the Template) inherits this style as well (Padding 3 etc.)
Is there any way to say the controls in the ControlTemplate not to inhertit these global styles? Just to mention. It is a Generic Template

2

2 Answers

2
votes

Not sure if I understood you correctly. Did some work around as I had similar situation where all my control's styles specified globally and inherited automatically without x:key.

I had to remove styles for some controls and apply different style for some controls.

In that situation I have created separate style (Below e.g. wants remove styles) so just created Empty style and used where ever and to which ever control it was required.

Empty Style:

<Grid.Resources>
    <ResourceDictionary>
        <Style x:Key="EmptyStyle"/>
    </ResourceDictionary>
</Grid.Resources>

Apply to Control:

<TextBlock Text="First Name" Style="{StaticResource EmptyStyle}" />
0
votes

You can put your Style in a resource dictionary with a x:Key, I've always do this and my style is applied only if I specify the key on my control.