wpfdynamicresourcevisualbrush

Can I set dynamic Resource's property as Dynamic Resource, and when I change property resource, parent resource will change also?


I have a VisulBrush like this:

<VisualBrush
    x:Key="visualBrush"
    TileMode="Tile"
    Viewbox="0,0,5 5"
    ViewboxUnits="Absolute"
    Viewport="0,0,5,5"
    ViewportUnits="Absolute">
    <VisualBrush.Visual>
        <Path
            Data="M 0 5 L 5 0 M -2 2 L 2 -2 M 3 7 L 7 3"
            RenderOptions.EdgeMode="Aliased"
            Stroke="Black"
            StrokeEndLineCap="Square" />
    </VisualBrush.Visual>
</VisualBrush>

You can see this visualBrush create strip lines.And I set stripe line's color to "Black",now it looks like this:

enter image description here

Now I can bind any UIElement's Backgroud with this VisualBrush.But can I change stripe line's Color dynamic ?

For example: when I have a Grid which Background property has bound to this VisualBrush,and now it's stripe line is Black,I hope when I press a button,this Grid's Background stripe can turn to "yellow".

I tried many ways,but none works.Please give me some advice.Thanks.


Solution

  • Dynamicresource will only "notify" on the top level object change.

    It's a sort of hashtable and when you set the value ( switch out the entire object ) then the UI will pick that up.

    Assuming changing the whole brush out isn't going to suit you.

    You can bind a property, but it'd have to notify changes. Meaning a property in a class that implements inotifypropertychanged or a dependency property.

    For a dependency property you could use an attached property on the visualbrush and bind to that.

    Or you could use styling to set it, give a default value and use a trigger or datatrigger or visual state to change the property ( color ). Which is of course how the likes of mouse over and disabled styling works in control templates.