Search code examples
.netwpfstylestargettype

Applying style by TargetType, if style connected in App.xaml


For example, I have style

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

in file Button.xaml If I add this style using MergeDictionary in file where i want to use it - all working good. But if i connect this style higher in the hierarchy of files (for example in App.xaml) style stop working until i set x:Key for it and for all button set

Style="{StaticResource name_of_style}".

Can i avoid this behavior or I must set for all my elements style by name?


Solution

  • Try setting it as an application resource within your app.xaml, you can also do the same thing when calling specific styles to specific views if you do the same to the UserControl.Resources of your view.

    PS - Example is silverlight, but method remains the same.

    <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                  x:Class="The.App">
        <Application.Resources>
            <Style TargetType="Button" BasedOn="{StaticResource name_of_style}"/>
        </Application.Resources>