如何在后面的code使用DynamicResource?在后面、code、DynamicResource

2023-09-03 16:35:18 作者:爱是包容

我希望能够属性编程设置为一个动态资源。

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.

感谢您的回应,

推荐答案

一个静态的资源不会更新不管你做它在code或XAML。你需要一个动态的资源这一点。

A static resource won't update whether you do it in code or XAML. You'll need a dynamic resource for that.

在XAML:

<Grid x:Name="grid" Background="{DynamicResource Brush}"/>

在code:

grid.SetResourceReference(Grid.BackgroundProperty, "Brush");