Search code examples
wpfxamlresourcedictionary

Edit ResourceDictionary from an external assembly


I've been searching around if it is possible for an assembly to change the ResourceDictionary values from another assembly at runtime. So far I've found nothing. Here´s the deal. I have a UserControl that will work independently so it can be fit into different projects. My UserControl has its own Resources.xaml (Compiled as Resources). I have a second assembly that it used as a setup tool for this user control. It basically just reads the UserControl Resources.xaml (which is working great) and then replaces the values of the Resources.Xaml. Trouble is, I cannot change the resource values.

Here's the code I use on my setup tool to read the ResourceDictionary:

    <Window.Resources>
     <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Control;component/Configuration/Resources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
    </Window.Resources>

To change this values I've tried something like:

this.Resources.MergedDictionaries.First()["IsZoomable"] = false;

It does recognize the resources but it wont change it. Is it even possible to change the values of the dictionary of another assembly at runtime or will I need to create an external dictionary that can be acessed by both assemblies?

EDIT:

  • The user control contains the Resources.xaml. I've defined the Resources in this control like this:

    <UserControl.Resources>
     <ResourceDictionary Source="Configuration/Resources.xaml"/>
    </UserControl.Resources>
    
  • The setup tool can acess this resources but I'm not able to change them. When I do, the user control still reads the old values. Resources are defined in the setup tool like so:

    <Window.Resources>
      <ResourceDictionary Source="pack://application:,,,/Control;component/Configuration/Resources.xaml"/>
    </Window.Resources>
    

Solution


  • Yes, you can change the value of a resource at runtime. Since you have merged the dictionary, you can change the value like this:

    this.Resources["IsZoomable"]=false;
    

    Make sure that the resource key matches the actual key.
    If you have merged the dictionary in app.xaml, then you can use:

    Application.current.Resources["IsZoomable"]=false;