9

I have a visual brush which is a group of shapes, the main colour of which is a dynamic resource itself - so the shape is for example MyShape and the Colour, MyColour which is referenced by the Shape object.
My problem is when I update the colour for this - it only happens the first time the shape is loaded (the colour needs to be set first) however as much as I change the colour it won't update the dynamic resource that uses the colour - how do I make this work?
Just need to make a dynamic resource work within another dynamic resource and have them both update when I change the colour.
I have no idea how to get this to work - I spent time creating a colour-picker for WPF only to find I cannot change the colour of this item - 1-Tier resources work where I set the brush/colour directly but not a colour within another object or 2-Tier Resource.

Edit: My problem seems to be specific to using these in a seperate Resource / Dictionary as my program needs to access this item from a class not the Window, the main example mentioned does not work when the MyColor is in a seperate Resource.

RoguePlanetoid
  • 4,516
  • 7
  • 47
  • 64

2 Answers2

7

Unless I misunderstand the situation, exactly what you're talking about works pretty well. I just tried it out with this Xaml:

<Window x:Class="ConditionalTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <SolidColorBrush x:Key="MyColor" Color="Aqua" />

        <VisualBrush x:Key="MyBrush">
            <VisualBrush.Visual>
                <Ellipse Height="50" Width="100" Fill="{DynamicResource MyColor}" />
            </VisualBrush.Visual>
        </VisualBrush>
    </Window.Resources>
    <Grid Background="{DynamicResource MyBrush}">
        <Button Height="30" Width="Auto" VerticalAlignment="Center" HorizontalAlignment="Center" Content="ChangeColor" Click="Button_Click" />
    </Grid>
</Window>

And then changed the color in the click handler for that button:

private void Button_Click(object sender, RoutedEventArgs e)
{
    ((SolidColorBrush)Resources["MyColor"]).Color = Colors.Purple;
}  

And it worked like a champ.

MojoFilter
  • 12,256
  • 14
  • 53
  • 61
  • 1
    Thanks for this - this does not work however when the Resource is in a Resource Dictionary this seems to be my problem I moved the resources to the Window.Resources like your example - this worked however this must be usable by a class in my software - will edit my question to mention this – RoguePlanetoid Feb 17 '09 at 19:49
  • My solution was to store the resource the Window.Resources as this example shows instead and this worked as your example did - but would still have liked to keep all resources in Resources.xaml, but it works and that is what matters. – RoguePlanetoid Feb 24 '09 at 10:36
  • 2
    Did anyone ever discover how to use this within a Resource Dictionary? – tofutim Oct 11 '11 at 18:35
  • Is the ResourceDictionary you're referring to in the same assembly, or in a different assembly and you're trying to merge it in, etc.? – Mark A. Donohoe May 23 '15 at 09:05
  • @tofutim I was able to refresh the `DynamicResource` by removing the `ResourceDictionary` that used the `DynamicResource` and adding it again. Basically refreshing the resource by force. Not the best way. https://stackoverflow.com/questions/63852845/drawingbrush-geometrydrawing-with-dynamic-brush-property/63853395?noredirect=1#comment112916415_63853395 – Nicke Manarin Sep 12 '20 at 17:37
0

Can you post an example of how you are attempting to change the color in the resource dictionary?

When I make a sample app and try to change the resource value it appears that the SolidColorBrush in the resource dictionary has been frozen so it can't be modified. To get around this I just set the new value to a new SolidColorBrush.

Caleb Vear
  • 2,637
  • 2
  • 21
  • 20
  • Creating a new brush each time would work however this negaates the use of a Dynamic Resource as recreating the resource each time for a complex shape with colour support is not as simple as Dim Item as New SolidColorBrush(MyColor). – RoguePlanetoid Feb 21 '09 at 14:05
  • If the brush has been frozen then the only way to change it is to make a new one. You can use the Clone method which will make a copy, that way you only have to change the properties you want to change. Can you post an example of how you tried to change the brush? – Caleb Vear Feb 22 '09 at 02:11