0

I'm using a theme that is supplied in one of the dlls my project references, like so :

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/SomeNameSpace;component/Themes/SomeTheme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

There happens to be a brush in this theme that I'd like to modify. If I know the brushes 'key', (found by using snoop), is it possible to redefine it to have a have a different colour?

I tried the following, but it had no effect.

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/SomeNameSpace;component/Themes/SomeTheme.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <LinearGradientBrush x:Key="keyNameFoundUsingSnoop">
                ...new value
            </LinearGradientBrush>
        </ResourceDictionary>
    </Application.Resources>

I was wondering if this type of thing is possible, for when either you dont have the xaml to recompile, or you'd like to modify key values on the fly.

Edit : I was unable to get this to work in XAML, but managed to get something similar in code :

void ReplaceResource(ResourceDictionary _res, object _key, object _replacement)
    {
        foreach (var i in _res.Keys)
        {
            if (object.Equals(i, _key))
            {
                _res[i] = _replacement;
            }
        }

        foreach (var i in _res.MergedDictionaries)
        {
            ReplaceResource(i, _key, _replacement);
        }
    }

(I don't break when I find the key as in my case it was in multiple dictionaries)

Usage :

var lightGray = new SolidColorBrush(Color.FromArgb(50, 255, 255, 255));
ReplaceResource(Application.Current.Resources, "KeyToFind", lightGray);
SeyedPooya Soofbaf
  • 2,654
  • 2
  • 29
  • 31
pastillman
  • 1,104
  • 2
  • 16
  • 27

0 Answers0