1.增加外部样式文件,添加资源词典(WPF)文件创建公共样式文件GlobalStyles.xaml
编写样式文件,以<style>标记开头,TargetType属性为控件类型,如按钮“Button”,单选按钮“RadioButton”等,x:Key属性自定义命名,控件在引用属性的时候需将Style属性设置为{StaticResource key属性}
<Style TargetType="Button" x:Key="NormalButtonStyle"> <Setter Property="FontFamily" Value="../Fonts/#iconfont"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Background="Transparent" Name="back"> <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" Value="#22FFFFFF" TargetName="back"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="#22FFFFFF" TargetName="back"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
2.其他页面使用该公共样式资源
添加<Window.Resources>,在该节点下添加<ResourceDictionary>,在该节点下添加<ResourceDictionary.MergedDictionaries> ,在该节点下添加<ResourceDictionary Source="./Assets/Styles/GlobalStyles.xaml"/>引入外部资源文件
在控件上通过style属性引用外部样式资源文件
标签:控件,文件,样式,添加,WPF,页面,属性 From: https://www.cnblogs.com/jiangyuhu/p/18647905