wpf后台修改resource的style

有一个功能需要在后台修改App.xaml中的<Application.Resources>的Style

App.xaml中如下

<Style x:Key="firstColor" TargetType="Border">
    <Setter Property="Background" Value="{DynamicResource color1}" />
</Style>

后台修改

//创建一个样式
Style style = new Style();
style.TargetType = typeof(Border);
style.Setters.Add(new Setter(Border.BackgroundProperty, Brushes.Transparent));
//修改App.xaml的Application.Resources的Style
Application.Current.Resources["firstColor"] = style;

前台

<Border Style="{DynamicResource firstColor}" Grid.Row="1" Grid.Column="0"></Border>

前台style这里要设置为DynamicResource ,StaticResource后台修改后忘了是报错还是不生效

参考:

Style后台动态定义[转]

WPF 资源(StaticResource 静态资源、DynamicResource 动态资源)

posted @ 2022-12-30 16:41  户的博客  阅读(873)  评论(0编辑  收藏  举报