Dynamically binding to a logical resource
As we saw in the previous recipe, Using logical resources, binding to a resource is achieved in XAML by using the StaticResource
markup extension. But what happens if we replace a specific resource? Would that be reflected in all objects using the resource? And if not, can we bind to the resource dynamically?
Getting ready
Make a copy of the previous recipe project CH02.SimpleResources
, or create a new project named CH02.DynamicVsStatic
and copy the Window XAML contents from the previous project to the new one.
How to do it...
We'll replace StaticResource
with DynamicResource
and see the effect:
Open
MainWindow.xaml
. Add a button to the end of theStackPanel
labeled Replace brush and connect it to an event handler namedOnReplaceBrush
. This is the added markup:<Button Content="Replace brush" Click="OnReplaceBrush" />
In the event handler, we'll replace the brush resource named
brush1
with a completely new brush:void OnReplaceBrush(object...