0

in order to clean up my code, I'm trying to split my app.xaml into seperate resource dictionaries. This works at runtime, but not at design time:

snipped in app.xaml

    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/;component/Theme/Colors.xaml" />
            <ResourceDictionary Source="/;component/Theme/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Application.Resources>

Colors.xaml

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

  <SolidColorBrush x:Key="backgroundBrush" Color="Gold"/>
</ResourceDictionary>

Styles.xaml

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

      <Style TargetType="StatusBar">
        <Setter Property="Background" Value="{StaticResource backgroundBrush}" />
      </Style>
    </ResourceDictionary>

Snipped of MainWindow.xaml

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="test" Width="800" Height="600" >
    <StatusBar Name="statusBar" DockPanel.Dock="Bottom">
        <StatusBarItem Content="{Binding statusMessage}" />
    </StatusBar>

DesignView gives the error: Error 8 '{DependencyProperty.UnsetValue}' is not a valid value for property 'Background'. C:\Daten\DotNet\test\test\MainWindow.xaml 123

If I put backgroundBrush directly into app.xaml like so:

    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/;component/Theme/Colors.xaml" />
            <ResourceDictionary Source="/;component/Theme/Styles.xaml" />
            <ResourceDictionary>
                <SolidColorBrush x:Key="backgroundBrush" Color="Gold"/>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Application.Resources>

DesignView has no problems.

So is there a way to tell DesignView where to find backgroundBrush, if this brush is placed into a seperate resource dictionary?

MTR
  • 1,690
  • 3
  • 20
  • 47
  • Are your both ResourceDictionary files in same assembly or different assemblies? Becasue its working fine at my end if resource dictionaries are in same assembly. – Rohit Vats Oct 12 '11 at 13:12
  • Everything in the main assembly. – MTR Oct 13 '11 at 05:45

3 Answers3

5

Thats the problem with StaticResource isnt it. It needs the resource key resolved explicitly using shared \ merged \ direct resource dictionaries hierarchically up.

There are two options...

merge Colors.xaml dictionary in Styles.xaml

OR

in Styles.xaml refer the bursh using DynamicResource.

WPF-it
  • 19,625
  • 8
  • 55
  • 71
  • -Even with StaticResource it's working fine at my end. So, i guess issue is somewhere else. – Rohit Vats Oct 12 '11 at 13:20
  • ok, DynamicResource solved the problem. But to understand, please can you tell me what is the difference and will there be any impact? – MTR Oct 13 '11 at 05:42
  • The dynmaic resource isnt resolved using its "x:Key" until runtime. `DynamicResource`s could make your application slow if used in excessive manner. But they are gr8 to change application skins (colored themes) dynamically. – WPF-it Oct 13 '11 at 05:49
  • ok, so I will opt for your first option, because I don't want a slow application just because DesignView can't resolve static resources. – MTR Oct 13 '11 at 10:10
  • Just curious to ask, how many such resources do you have ... a few hundred is ok ... they should not be a problem as long as they are brushes or colors (being lightweight) and as long as we dont change merged \ shared \ referred dictionaries much. – WPF-it Oct 13 '11 at 10:16
  • I don't know at the moment, because I'm just evaluating for a huge future application. – MTR Oct 13 '11 at 11:52
1

Try

{StaticResource ApplicationPageBackgroundThemeBrush}

for your status bar background value

ChrisF
  • 134,786
  • 31
  • 255
  • 325
jacksonT
  • 23
  • 5
1

In case Resources are in different assemblies than that where MainWindow resides and the one dictionary is refering to the other dictionary. In that case reference is not resolved. This bug is already reported at Microsoft site in case your target framework is 4.0. However they have provided a workaround for it. Simply add the empty style in your Resource dictionaries and it will work fine like this -

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/;component/Theme/Colors.xaml" />
        <ResourceDictionary Source="/;component/Theme/Styles.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style TargetType="{x:Type Window}"/>
  </ResourceDictionary>
</Application.Resources>

For further refernce please look at this link - https://connect.microsoft.com/VisualStudio/feedback/details/555322/global-wpf-styles-are-not-shown-when-using-2-levels-of-references#details

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Doesn't help. My resource dictionaries all reside in the main assambly and I only get this error in DesignView. At runtime everything is ok. – MTR Oct 13 '11 at 05:30