I need to apply a style in code ike this:
TextBlock.Style = TryFindResource("MyStyle") as Style;
that will be updated dynamically when the resource dictionary is changed (i.e. skin is replaced at runtime). In other words I need the equivalent to using a dynamic resource like this:
<TextBlock Style="{DynamicResource MyStyle}" />
By calling element. style. color = "red"; you can apply the style change dynamically. Below is a function that turns an element's colour to red when you pass it the element's id .
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.
To add CSS properties dynamically, we use css() method. The css() method is used to change style property of the selected element.
Note that you can also change style of an element by getting a reference to it and then use its setAttribute method to specify the CSS property and its value. Be aware, however, that setAttribute removes all other style properties that may already have been defined in the element's style object.
Try using SetResourceReference.
textBlock.SetResourceReference(TextBlock.StyleProperty, "MyStyle")
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