Search code examples
c#wpfxamlresourcedictionarygeneric.xaml

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?


Solution

  • 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.