在wpf窗体/用户控件的xaml.cs代码中,给控件绑定样式
1.如果样式写在窗体/用户控件的xaml中(以checkbox绑定样式为例):
checkbox.Style = (Style)this.FindResource("CheckBoxStyle");
2.如果样式写在单独的资源文件中,如Styles.xaml,此时后台代码绑定样式如下(以checkbox为例):
string TemplateUri = "pack://application:,,,/StyleTestDemo;component/Resources/Styles.xaml";
ResourceDictionary dic = new ResourceDictionary { Source = new Uri(TemplateUri) };
checkbox.Style = dic["AdvancePayCheckBox"] as Style;
pack://application:,,,/StyleTestDemo;component/Resources/Styles.xaml
这个是资源文件的全局路径,其中StyleTestDemo
是资源样式所在的项目名称,component
代指项目模块,和后面的路径构成完整的文件路径。