打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
WPF XAML 为项目设置全局样式

全局资源样式属性

App.xaml

<Application.Resources> <ResourceDictionary><br> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Dictionary.xaml"/> </ResourceDictionary.MergedDictionaries><br> <Style x:Key="xxx" TargetType="Button"> <Setter Property="Foreground" Value="White"></Setter> <Setter Property="FontSize" Value="30"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid></Grid> </ControlTemplate> </Setter.Value> </Setter></Style> <ControlTemplate x:Key="xx" TargetType="Button"> <Grid></Grid> </ControlTemplate> </ResourceDictionary></Application.Resources>

说明:

1.行类属性尽量少用,只有特殊控件 需要用到行内属性,

   正确的做法是封装统一风格的所有控件。
  (例如按钮,统一高宽,字体,字体大小,然后申明到独立的资源字典中,
    在App.xaml中引用)

2.头部资源引用情况用于 不同 Window 适应不同主题或者风格的情况。

   比如为某一个窗口申明一个当前窗口单独使用的样式。

  (例如播放器的旋转控件,只有一个页面用到,只需要在Window级引用对应资源字典)

   不放在App.xaml原因是为了降低内存消耗。

3.App.xaml 里面的资源引用适用于全局资源。理论上每一个被申明的Window
   都会创建一个对应资源字典的实例。除非是每个Window都会用到的模块,
    不然建议放到对应Window级

经典实例:

ControlStyle.xaml<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DemoForm.UI"> <Style x:Key="BtnControl" TargetType="Button"> <Setter Property="FontSize" Value="15"/> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Height" Value="40"/> <Setter Property="Margin" Value="2"/> <!--<Setter Property="Background" Value="Red"/>--></Style></ResourceDictionary>
App.xaml<Application x:Class="DemoForm.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DemoForm" StartupUri="MainWindow.xaml">
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/UI/ControlStyle.xaml"></ResourceDictionary> <!--或者这样方式DemoForm;component/UI/Dictionary1.xaml 引用以后就可以继承了--> </ResourceDictionary.MergedDictionaries> <Style BasedOn="{StaticResource BtnControl}" TargetType="Button" > <Setter Property="FontSize" Value="10" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Height" Value="40" /> <Setter Property="Margin" Value="2" /> <Setter Property="Template"> <!--应用于全局的控件模板--> <Setter.Value> <ControlTemplate TargetType="Button"> <Border BorderThickness="1" CornerRadius="10" Background="{TemplateBinding Background}"> <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> </Border> <ControlTemplate.Triggers > <Trigger Property="Button.IsMouseOver" Value="True"> <Setter Property="Button.Background" Value="blue"/> </Trigger > </ControlTemplate.Triggers > </ControlTemplate> </Setter.Value> </Setter></Style> <Style TargetType="Label"> <!--//x:Key="LblStyle"去掉就是全局引用--> <Setter Property="FontSize" Value="12" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /></Style> <Style TargetType="TextBox"> <!--//x:Key="TxtStyle" 去掉就是全局引用--> <Setter Property="FontSize" Value="12" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="MaxHeight" Value="50" /> <Setter Property="MinWidth" Value="80" /> <Setter Property="Margin" Value="2" /></Style> <!--<ControlTemplate x:Key="buttonTemplate" TargetType="Button" > <Border BorderThickness="1" CornerRadius="10" Background="{TemplateBinding Background}"> <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> </Border> <ControlTemplate.Triggers > <Trigger Property="Button.IsMouseOver" Value="True"> <Setter Property="Button.Background" Value="blue"/> </Trigger > </ControlTemplate.Triggers > </ControlTemplate >--> </ResourceDictionary> </Application.Resources></Application>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
闲话WPF之二五(WPF中的ControlTemplate [3])
C# WPF 一个设计界面
WPF自定义按钮显示效果
WPF自定义控件与样式(7)
WPF 详解模板
(转) silverlight 样式学习
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服