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 @   户的博客  阅读(982)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2020-12-30 Windows10远程桌面连接提示:出现身份验证错误,要求的函数不受支持
点击右上角即可分享
微信分享提示