1.从页面资源中获取样式并应用

btnTest.Style = (Style)this.Resources["BigButtonStyle"]

2.从项目中单独分开的资源字典文件(或一个引用的程序集中)中获取样式。

首先,需要创建一个ResourceDictionary对象并提供正确的URI:

ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Source = new Uri("/Test.xaml",UriKind.Relative);

假设样式字典的名字叫Test.xaml,资源字典的编译格式必须为内容(将属性窗口的Build Action设置为Content)

在配置好ResourceDictionary后,就可以从获取样式并应用它:
 btnTest.Style = (Style)dictionary["SmallButtonStyle"];

3.移除样式

最后,要想移除样式,设置Style属性为空就可以。

btnTest.Style = null

 

 

相关文章:

  • 2022-12-23
  • 2021-06-21
  • 2021-12-04
  • 2021-06-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-24
  • 2021-11-30
  • 2021-06-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案