How do I apply a dynamic style in code at runtime?

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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}" /> 

回答1:

Try using SetResourceReference.

textBlock.SetResourceReference(TextBlock.StyleProperty, "MyStyle") 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!