zoukankan      html  css  js  c++  java
  • 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的主要原因是涉及同类控件扩展多,例如不同颜色。

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

  • 相关阅读:
    newifi3-D2 openwrt挂载u盘扩容/overlay
    # mac使用homebrew安装jdk和tomcat
    React学习小记--setState的同步与异步
    [vscode直接运行js文件报错]: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
    # js权威指南之对象篇
    # 初体验之腾讯位置服务彩云天气开发者api
    自用资源合集(持续更新)
    网页嵌入播客
    网页嵌入音乐歌单
    All live
  • 原文地址:https://www.cnblogs.com/SunsetAzure/p/6198214.html
Copyright © 2011-2022 走看看