程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WPF's Style BasedOn,wpfstylebasedon

WPF's Style BasedOn,wpfstylebasedon

編輯:關於.NET

WPF's Style BasedOn,wpfstylebasedon


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的主要原因是涉及同類控件擴展多,例如不同顏色。

再來就是為了統一基礎樣式,例如字體大小、樣式、顏色,外邊框以及水平垂直對稱的方式。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved