Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update Dynamic Resource within a Dynamic Resource?

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.

like image 744
RoguePlanetoid Avatar asked Feb 17 '09 18:02

RoguePlanetoid


People also ask

What is the difference between static resource and dynamic resource?

StaticResource are retrieved only once by the referencing element and used for entire life of the resource. On the other hand, DynamicResource are acquired every time the referenced object is used.

What is a dynamic resource?

Dynamic resources are resources that move along an assigned path network and may transport entities between locations as a forklift would. They may also need to process entities at several locations, such as an operator performing tasks at more than one location.

What are dynamic resources in WPF?

Utilizing WPF Resources 2. Dynamic Resource - Dynamic resources are the resources which you can manipulate at runtime and are evaluated at runtime. If your code behind changes the resource, the elements referring resources as dynamic resources will also change.


1 Answers

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.

like image 162
MojoFilter Avatar answered Nov 15 '22 23:11

MojoFilter