0

I am trying to apply a theme to a bunch of wpf projects. There is one assembly containing the generic.xaml and different applications. As far as i understand i can't use the ThemeInfo attribute with ResourceDictionaryLocation.ExternalLocation because the name have to be the same as my program but I have more than one program...

So I search and found that I only have to include the dictionary as MergedDictionary in the app.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/ClassLibrary1;component/Themes/generic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

This basically works. But if I use a style for the controls it will not apply the generic.xaml style anymore:

generic.xaml in ClassLibrary1.dll

<ResourceDictionary x:Class="ClassLibrary1.Themes.generic"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style TargetType="{x:Type Button}">
    <Setter Property="Background"
            Value="Black" />
</Style>

Window in program

<Window x:Class="Theming.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style TargetType="Button"
               x:Key="ButtonGreenTextStyle">
            <Setter Property="Foreground"
                    Value="Green" />
        </Style>
    </Window.Resources>

    <Grid>
        <Button Style="{DynamicResource ButtonGreenTextStyle}" Content="Test" />
    </Grid>
</Window>

What I have to do, that WPF will understand the style in my generic.xaml as basestyle for all buttons (I know I will also have to write a ControlTemplate; the above code is just for simplicity)

Daniel Bişar
  • 2,663
  • 7
  • 32
  • 54

2 Answers2

0

Two things I would try

  • Create a generic ButtonBase style/template to set the look of all buttons
  • Try using a BasedOn attribute on the ButtonGreeTextStyle, basing it on an existing style.
felixthehat
  • 849
  • 2
  • 9
  • 24
  • As far as I know WPF would not apply templates for BaseClasses. BasedOn would work but I was wondering if WPF should not apply the Style as default because it is defined in the generic.xaml – Daniel Bişar Dec 14 '11 at 09:48
0

I found another solution. You have to write styles based on a custom markup:

This will apply the current theme style. The code for this MarkupExtension can be found here:

How do I alter the default style of a button without WPF reverting from Aero to Classic?

Community
  • 1
  • 1
Daniel Bişar
  • 2,663
  • 7
  • 32
  • 54