WPF's Style BasedOn

简介: 原文:WPF's Style BasedOn 1 2 3 4 5 6 All Style Based 1 2 3 4 5 6 7 8 9 ...
原文: WPF's Style BasedOn

1 <Style x:Key="BasedStyle" BasedOn="{x:Null}" TargetType="{x:Type Control}">
2     <Setter Property="FontFamily" Value="Microsoft YaHei" />
3     <Setter Property="FontSize" Value="12" />
4     <Setter Property="Foreground" Value="White" />
5     <Setter Property="FocusVisualStyle" Value="{x:Null}" />
6 </Style>
All Style Based
 1 <!--引用 BasedStyle-->
 2 <ResourceDictionary.MergedDictionaries>
 3     <ResourceDictionary Source="BasedStyle.xaml" />
 4 </ResourceDictionary.MergedDictionaries>
 5 
 6 <!--示例控件Style Based代码 以下示例为Button-->
 7 <Style x:Key="ButtonBaseBaseStyle" BasedOn="{StaticResource BasedStyle}" TargetType="{x:Type ButtonBase}">
 8     <Setter Property="Height" Value="45" />
 9         <Setter Property="Foreground" Value="{DynamicResource ButtonText}" />
10     <Setter Property="Padding" Value="0" />
11     <Setter Property="Margin" Value="0" />
12     <Setter Property="BorderThickness" Value="1" />
13     <Setter Property="HorizontalAlignment" Value="Center" />
14     <Setter Property="VerticalAlignment" Value="Center" />
15     <Setter Property="HorizontalContentAlignment" Value="Center" />
16     <Setter Property="VerticalContentAlignment" Value="Center" />
17 </Style>
18 
19     <Style x:Key="ButtonBaseStyle" BasedOn="{StaticResource ButtonBaseBaseStyle}" TargetType="{x:Type Button}">
20         <Setter Property="Template">
21             <Setter.Value>
22                 <ControlTemplate TargetType="{x:Type Button}">
23                     <Grid>
24                         <Border x:Name="border" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" />
25                         <ContentPresenter Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="true" />
26 
27                         <Rectangle x:Name="EnabledLayer" Fill="#4CFFFFFF" Visibility="Collapsed" />
28                     </Grid>
29                     <ControlTemplate.Triggers>
30                         <Trigger Property="IsMouseOver" Value="true">
31                             <Setter Property="Background" Value="{DynamicResource ButtonBackgroundHover}" />
32                         </Trigger>
33                         <Trigger Property="IsPressed" Value="true">
34                             <Setter Property="Background" Value="{StaticResource ButtonBackgroundPressed}" />
35                         </Trigger>
36                         <Trigger Property="IsEnabled" Value="false">
37                             <Setter TargetName="EnabledLayer" Property="Visibility" Value="Visible" />
38                         </Trigger>
39                     </ControlTemplate.Triggers>
40                 </ControlTemplate>
41             </Setter.Value>
42         </Setter>
43     </Style>
44 
45 <!--示例控件扩展 Style-->
46 <Style x:Key="Success" BasedOn="{StaticResource ButtonBaseStyle}" TargetType="{x:Type Button}">
47         <Setter Property="Background" Value="{DynamicResource ButtonBackground-Success}" />
48         <Setter Property="BorderBrush" Value="{DynamicResource ButtonBorder-Success}" />
49         <Style.Triggers>
50             <Trigger Property="IsMouseOver" Value="true">
51                 <Setter Property="Background" Value="#449d44" />
52                 <Setter Property="BorderBrush" Value="#398439" />
53             </Trigger>
54         </Style.Triggers>
55     </Style>
示例Style

引用

https://msdn.microsoft.com/zh-cn/library/system.windows.style.basedon(v=vs.110).aspx

https://github.com/ptddqr/bootstrap-wpf-style

总结

使用BasedOn的主要原因是涉及同类控件扩展多,例如不同颜色。

再来就是为了统一基础样式,例如字体大小、样式、颜色,外边框以及水平垂直对称的方式。

目录
相关文章
|
C# 前端开发
WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
原文:WPF中Style文件的引用——使用xaml代码或者C#代码动态加载   WPF中控件拥有很多依赖属性(Dependency Property),我们可以通过编写自定义Style文件来控制控件的外观和行为,如同CSS代码一般。
4625 0
|
C#
WPF QuickStart系列之样式和模板(Style and Template)
原文:WPF QuickStart系列之样式和模板(Style and Template) 在WPF桌面程序中,当我们想构建一个统一的UI表现时(在不同操作系统下,显示效果一致),此时我们就需要使用到WPF中的样式和模板技术。
1008 0
|
C#
WPF 自定义Metro Style窗体
原文:WPF 自定义Metro Style窗体 为了使WPF程序在不同版本的操作系统上保持一致的显示效果,我们需要重写WPF控件样式。这篇博客将展示如何创建一个Metro Style的WPF窗体。 首先先看一下最终窗体的效果图, 通过截图我们可以看出来这个窗体由两部分组成,顶部为最小化和关闭按钮,其他区域为窗体的显示区域。
1297 0
[Songqw.Net 基础]WPF插件化中同步Style
原文:[Songqw.Net 基础]WPF插件化中同步Style 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/article/details/50910590 ...
815 0
|
C#
【msdn wpf forum翻译】TextBlock等类型的默认样式(implicit style)为何有时不起作用?
原文:【msdn wpf forum翻译】TextBlock等类型的默认样式(implicit style)为何有时不起作用?原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/148e95c6-6fb5-4399-8a56-4...
957 0
|
C# 前端开发
WPF 中style文件的引用
原文:WPF 中style文件的引用 总结一下WPF中Style样式的引用方法: 一,内联样式: 直接设置控件的Height、Width、Foreground、HorizontalAlignment、VerticalAlignment等属性。
935 0
|
移动开发 前端开发 C#
Bootstrap WPF Style,Bootstrap风格的WPF样式
原文:Bootstrap WPF Style,Bootstrap风格的WPF样式 简介 GitHub地址:https://github.com/ptddqr/bootstrap-wpf-style 此样式基于bootstrap-3.
1409 0
|
C#
WPF's Style BasedOn
原文:WPF's Style BasedOn 1 2 3 4 5 6 All Style Based 1 2 3 4 5 6 7 8 9 ...
940 0
|
C# 前端开发
WPF中Style的使用
转自博客:    http://blog.csdn.net/liusanchun/article/details/6857558     Styel在英文中解释为”样式“,在Web开发中,css为层叠样式表,自从.net3.0推出WPF以来,WPF也有样式一说,通过设置样式,使其WPF控件外观更加美化同时减少了大量的复杂属性的设置。
1328 0