Question

I cannot get a button style to work in a Prism 4.0 app with four modules. Here is the button element from the xaml view file in Module2:

<Button Name="add" Width ="60" Style="{DynamicResource Red}"  Click="add_Click"> Add</Button>

The app builds and runs but the button color does not appear. I have defined the style in the Generic.xaml file in the Themes folder of the Shell Module. That is supposed to be where one can place styles to be shared between the modules. In the Generic.xaml file I have:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Shell.Controls" 
    xmlns:wc="clr-namespace:System.Windows.Controls;assembly=PresentationFramework">

 <Style 
   x:Key="{ComponentResourceKey 
    TypeInTargetAssembly={x:Type wc:Button},
    ResourceId=Red}"
    TargetType="wc:Button">
    <Setter Property="Foreground" Value="Red" />
  </Style>
</ResourceDictionary>

I also have the necessary reference in the AssemblyInfo.cs file in the Properties folder of the Shell Project. This should direct the Prism app to resolve all styles references from the Prism Generic.xaml file:

[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

Those are still the original settings that were provided by the Prism WPF Unity template with which I started, provided by David Hill's blog [http://blogs.msdn.com/b/dphill/archive/2011/01/16/prism-4-0-template-pack-now-available.aspx]. The template came with some styles already in Generic.xaml but the bare template app only used those styles for controls in the Shell assembly, hence the parameters, "None" and "SourceAssembly" shown above.

Since I am trying to define styles for use in modules other than the Shell module, I added the following ThemeInfo attribute to the AssemblyInfo.cs of Module1 and Module2L

[`assembly: ThemeInfo(ResourceDictionaryLocation.ExternalAssembly, ResourceDictionaryLocation.ExternalAssembly)]`

I Tried adding a ThemeDictionary extension to App.xaml like this in App.xaml & no result.

    <Application.Resources>
    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="{ThemeDictionary MyApp}"/>
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    </Application.Resources>

Also tried a pack url like this in App.xaml & got "Cannot locate resource" error.

    <Application.Resources>
    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MyApp;Shell/Themes/Generic.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    </Application.Resources>

Any thoughts or ideas as to what is missing? Thank you.

Was it helpful?

Solution

UPDATE: I got the answer from Marc Rodman at Microsoft through technical support. Two changes:

One was in Generic.xaml, changing the style definition to:

    <Style
x:Key="Red"
TargetType="Button">
    <Setter Property="Foreground" Value="Red" />
    </Style>

The other change was to App.xaml:

  <Application.Resources>

    <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:,,,/Shell;component/Themes/Generic.xaml"/>
     </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
    </Application.Resources>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top