Override Generic.xaml Resource Dictionary resources from another project in same solution

I have a solution in which I have a Windows Phone App project and a ClassLibrary that itself has TemplatedControl.cs whose default style is referenced in the ClassLibrary/Themes/Generic.xaml Resource Dictionary.

- MySolution
    - WPApp_Project
        - MainPage.xaml/MainPage.xaml.cs
        - App.xaml/App.xaml.cs

    - ClassLibrary_Project
        - Themes
            - Generic.xaml
        - View
            - TemplatedControl.cs
        - ViewModel
            - TemplatedViewModel.cs
            - ViewModelLocator.cs

For instance, I defined this in the Generic.xaml file:

<SolidColorBrush x:Key="MyBrush" Color="Gold"/>

How can I, from my WP App, change the value of this SolidColorBrush to an extent that it affects every element in my ClassLibrary that uses this Resource?

Upvotes: 0

Views: 892

Answers (2)

landoncz
landoncz

Reputation: 2017

You basically need to just override the x:Key with another value AFTER the generic value is already loaded. You can do this by dynamically loading a resource files that contains your keys you want to override.

To dynamically load XAML resource dictionaries, you can do something like this:

Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/WPF.Common.UI;component/Resources/Dictionaries/ApplicationResourceDictionary.xaml") });

Upvotes: 1

I managed to find a way to do this. Since I have a TemplatedViewModel.cs with properties attached to it in my Generic.xaml I basically created extra properties for the things I want to change, i.e., if I want to change the color of a TextBlock, I add the following in my Generic.xaml:

<TextBlock Content="I am a TextBlock" Foreground="{Binding Path=HappyColor}" />

And in my WPApp I get an instance of my TemplatedViewModel.cs and set it to a new color:

TemplatedViewModel viewModel = ViewModelLocator.TemplatedStatic;
viewModel.HappyColor = "#00FF00";

Ideally I would like to be able to set properties in a ResourceDictionary in XAML, however I wasn't able to. So for the next few days if someone can tell me how to that, I'll mark their reply as the answer.

Upvotes: 0

Related Questions