Home > Enterprise >  Why is my icon color not changing when I change the base resource
Why is my icon color not changing when I change the base resource

Time:01-14

I have an icon defined as:

<DrawingImage x:Key="CloseIcon">
    <DrawingImage.Drawing>
        <DrawingGroup ClipGeometry="M0,0 V27 H28 V0 H0 Z">
            <DrawingGroup.Transform>
                <TranslateTransform X="3.9828000068664551" Y="0" />
            </DrawingGroup.Transform>
            <DrawingGroup Opacity="1" Transform="1,0,0,1,0.885057,0.5">
              <DrawingGroup Opacity="1" Transform="...">
                <GeometryDrawing Brush="{DynamicResource PrimaryBrush}" Geometry="..." />
              </DrawingGroup>
              <DrawingGroup Transform="...">
                 <GeometryDrawing Brush="{DynamicResource PrimaryBrush}" Geometry="..." /> 
              </DrawingGroup>
            </DrawingGroup>
        </DrawingImage.Drawing>
    </DrawingImage>

I have simplified the above xaml, there are a ton more nested DrawingGroups than in the actual code.

At run time, I am trying to do the following: The PrimaryBrush resource is set on load.

Then later on in the application I am trying to do the following:

Application.Current.Resources["PrimaryBrush"] = new SolidColorBrush(Colors.Green);

But the icon remains the same color. My understanding was that anything that had a DynamicResource would change when you changed that dynamic resource.

How do change the icon color at runtime?

(fyi, I have spent at least a week investigating and trying to figure this out, and to no avail),

Update (1/13/2023 14:10 EST

  • The DrawingImage is defined in a file called IconResourcs.xaml
  • This file is included via ResourceDictionary.MergedDictionaries in the Resources.xaml file of an assembly that gets loaded at application start.

CodePudding user response:

DrawingImage extends Freezable. To allow the modification of nested Freezable resources you must define them with the x:Shared attribute set to false:

<DrawingImage x:Key="CloseIcon"
              x:Shared="False">
</DrawingImage>
  • Related