locked
Why doesn't ThemeDictionary fall back to Themes\Generic.xaml? RRS feed

  • Question

  • We have a WPF 3.5 SP1 application that uses the ThemeDictionary markup extension. Recently, we have noticed that ThemeDictionary does not automatically default to Themes\Generic.xaml if there is no theme file for the currently selected theme. Is this a bug in WPF 3.5 SP1, and is there any workaround?

    Here is an example for reproducing this behavior:

    XAML file for window in main application:

    <Window x:Class="ThemeTest.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300">
      <Window.Resources>
        <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="{ThemeDictionary MyCustomControl}"/>
          </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
      </Window.Resources>
      <Grid>
        <TextBlock Foreground="{StaticResource MyColor}">This is a test</TextBlock>
      </Grid>
    </Window>
    
    
    

    Themes\Generic.xaml file for custom control DLL named "MyCustomControl.dll":

    <ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:MyCustomControl">
      
      <SolidColorBrush x:Key="MyColor" Color="Red"/>
      
      <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
              <Border Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}">
    
              </Border>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </ResourceDictionary>
    
    
    

    When starting the main application, the following exception occurs:

      System.Windows.Markup.XamlParseException occurred
      'System.Windows.ThemeDictionaryExtension' value cannot be assigned to property 'Source' of object 'System.Windows.ResourceDictionary'. Cannot locate resource 'themes/luna.metallic.xaml'.  Error at object 'System.Windows.ThemeDictionaryExtension' in markup file 'ThemeTest;component/window1.xaml' Line 8 Position 37.

    Regards
    - Frank

    Tuesday, June 28, 2011 8:18 AM

Answers

  • Hi Frank,

    I can reproduce this problem in 3.5SP1 and the problem seems to be fixed in 4.0.

    I suggest you to create a resource file for each theme and merge generic.xaml to each theme specific resoruce dicionary to resolve this issue.

    For example,

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Generic.xaml"/>
     </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by fmunkert Wednesday, June 29, 2011 8:12 AM
    Wednesday, June 29, 2011 8:05 AM