I'd like to be able to set a property to a dynamic resource programmatically.
myControl.Property = this.Resource[key]
is not a valid response, since if the resource with the key 'key' is replaced, the property is not updated automatically.
Thanks for you response,
A static resource won't update whether you do it in code or XAML. You'll need a dynamic resource for that.
In XAML:
<Grid x:Name="grid" Background="{DynamicResource Brush}"/>
In code:
grid.SetResourceReference(Grid.BackgroundProperty, "Brush");
Be aware that DynamicResource is not available in Silverlight; it is only in WPF (Silverlight only has StaticResource).
Since you tagged your question both Silverlight and WPF, I suspect that you may be looking for a solution that works in both. If that is the case, you will probably want to use data binding instead of resources since you require the property to update in response to changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With