C# - Dynamic Resources in WPF

Setting Dynamic Resource Keys

In this example, I needed to have a color value respected throughout a WPF app whenever it changed. Having the properties defined in a resource dictionary worked initially, but wouldn’t update when the value changed. This appears to be because UserControls that have a MergedDictionaries object set will effectively create a copy of those properties and won’t care if the original is changed.

private void SetApplicationColorResource(string key, string rgbColorField)
{
    string[] rgbValues = rgbColorField
        .Trim(new char[] { 'r', 'g', 'b', ')', '(', ' ' })
        .Split(',');
    Color configuredColor = Color.FromRgb(
        Convert.ToByte(rgbValues[0]),
        Convert.ToByte(rgbValues[1]),
        Convert.ToByte(rgbValues[2]));
    Application.Current.Resources[key] = configuredColor;
}
<Application x:Class="Launcher.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">
    <Application.Resources>
        <Color x:Key="Color_Booth_Primary">#d56103</Color>
        <Color x:Key="Color_Booth_PrimaryText">#d56103</Color>
    </Application.Resources>
</Application>

development c_sharp wpf