字体的自动继承的特性

  • Style 样式
  • ControlTemplate 控件模板

示例
1、演示字体的自动继承的特性
Controls/UI/FontInherit.xaml

  1. <Page
  2. x:Class="Windows10.Controls.UI.FontInherit"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.UI"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d"
  9.  
  10. FontSize="100">
  11.  
  12. <Grid Background="Transparent">
  13. <StackPanel Margin="10 0 10 10">
  14.  
  15. <!--
  16. 本例演示字体的自动继承的特性
  17. Font 相关的设置来自 Windows.UI.Xaml.Controls.Control
  18. -->
  19.  
  20. <!--
  21. 继承了 Page 的关于 Font 的设置
  22. -->
  23. <TextBlock Text="FontSize = 100" />
  24.  
  25. <UserControl FontSize="50">
  26. <!--
  27. 继承了 UserControl 的关于 Font 的设置
  28. -->
  29. <TextBlock Text="FontSize = 50" />
  30. </UserControl>
  31.  
  32. </StackPanel>
  33. </Grid>
  34. </Page>

2、演示“Style”相关知识点
Controls/UI/Style.xaml

  1. <Page
  2. x:Class="Windows10.Controls.UI.Style"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.UI"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Name="grid" Background="Transparent">
  11.  
  12. <!--
  13. 注:在 Grid.Resources 指定的资源(支持 ResourceDictionary 方式),其作用域仅在 Grid 之内,全局资源需要在 App.xaml 中配置
  14. -->
  15. <Grid.Resources>
  16.  
  17. <!--
  18. Style - 样式
  19. x:Key - 标识(不指定此值,则样式会应用于所有 TargetType 所指定的类型)
  20. TargetType - 目标对象类型
  21. BasedOn - 指定当前样式的父样式(此样式会继承指定的父样式)
  22. Setter - 属性设置器
  23. Property - 需要设置的属性名称
  24. Value - 需要设置的属性值
  25. -->
  26.  
  27. <!--
  28. 自定义一个基础样式
  29. -->
  30. <Style x:Key="TextBoxStyleBase" TargetType="TextBox">
  31. <Setter Property="Foreground" Value="Red"/>
  32. </Style>
  33.  
  34. <!--
  35. 这是自定义了全局样式,但是其他的自定义样式并不会自动继承这个自定义全局样式
  36. 所以,此处用 BasedOn 继承基础样式,然后其他自定义样式也继承基础样式就好(这就相当于继承了这个自定义全局样式)
  37. -->
  38. <Style TargetType="TextBox" BasedOn="{StaticResource TextBoxStyleBase}">
  39.  
  40. </Style>
  41.  
  42. <!--
  43. 不会自动继承上面的自定义全局样式
  44. -->
  45. <Style x:Key="TextBoxStyleBig1" TargetType="TextBox">
  46. <Setter Property="FontSize" Value="24"/>
  47. <Setter Property="Height" Value="60"/>
  48. </Style>
  49.  
  50. <!--
  51. 继承了基础样式(相当于继承了上面的自定义全局样式)
  52. -->
  53. <Style x:Key="TextBoxStyleBig2" TargetType="TextBox" BasedOn="{StaticResource TextBoxStyleBase}">
  54. <Setter Property="FontSize" Value="24"/>
  55. <Setter Property="Height" Value="60"/>
  56. </Style>
  57.  
  58. </Grid.Resources>
  59.  
  60. <StackPanel Margin="10 0 10 10">
  61.  
  62. <!--默认使用自定义全局样式-->
  63. <TextBox Name="textBox1" Text="我是 TextBox" Margin="5" />
  64.  
  65. <!--指定样式资源-->
  66. <TextBox Name="textBox2" Text="我是 TextBox" Margin="5" Style="{StaticResource TextBoxStyleBig1}" />
  67.  
  68. <!--动态改变 FrameworkElement 的样式-->
  69. <Button Name="btnChangeStyle" Margin="5" Content="改变样式" Click="btnChangeStyle_Click" />
  70.  
  71. <!--内联样式-->
  72. <TextBox Name="textBox3" Text="我是 TextBox" Margin="5">
  73. <ToolTipService.ToolTip>
  74. <ToolTip Name="toolTip" Content="ToolTipService.ToolTip" Placement="Bottom">
  75. <ToolTip.Style>
  76. <Style TargetType="ToolTip">
  77. <Setter Property="Foreground" Value="Blue" />
  78. </Style>
  79. </ToolTip.Style>
  80. </ToolTip>
  81. </ToolTipService.ToolTip>
  82. <TextBox.Style>
  83. <Style TargetType="TextBox">
  84. <Setter Property="Background" Value="Green"/>
  85. </Style>
  86. </TextBox.Style>
  87. </TextBox>
  88.  
  89. <!--在 c# 中定义样式-->
  90. <TextBox Name="textBox4" Text="我是 TextBox" Margin="5" />
  91.  
  92. </StackPanel>
  93. </Grid>
  94. </Page>

Controls/UI/Style.xaml.cs

  1. /*
  2. * 演示“Style”相关知识点
  3. *
  4. * 注:
  5. * 1、Style 属性来自 Windows.UI.Xaml.FrameworkElement
  6. */
  7.  
  8. using System;
  9. using Windows.UI;
  10. using Windows.UI.Xaml;
  11. using Windows.UI.Xaml.Controls;
  12.  
  13. namespace Windows10.Controls.UI
  14. {
  15. public sealed partial class Style : Page
  16. {
  17. public Style()
  18. {
  19. this.InitializeComponent();
  20.  
  21. this.Loaded += Style_Loaded;
  22. }
  23.  
  24. // 在 c# 中定义样式
  25. private void Style_Loaded(object sender, RoutedEventArgs e)
  26. {
  27. Windows.UI.Xaml.Style style = new Windows.UI.Xaml.Style();
  28. style.TargetType = typeof(TextBox);
  29.  
  30. Setter setter1 = new Setter();
  31. setter1.Property = TextBox.BackgroundProperty;
  32. setter1.Value = Colors.Blue;
  33.  
  34. style.Setters.Add(setter1);
  35.  
  36. textBox4.Style = style;
  37. }
  38.  
  39. // 改变样式
  40. private void btnChangeStyle_Click(object sender, RoutedEventArgs e)
  41. {
  42. // 获取 Application 中的资源
  43. // (Windows.UI.Xaml.Style)Application.Current.Resources["myStyle"];
  44.  
  45. // 获取关联 xaml 内的资源
  46. if (textBox2.Style == (Windows.UI.Xaml.Style)grid.Resources["TextBoxStyleBig1"])
  47. {
  48. // 指定样式
  49. textBox2.Style = (Windows.UI.Xaml.Style)grid.Resources["TextBoxStyleBig2"];
  50. }
  51. else
  52. {
  53. textBox2.Style = (Windows.UI.Xaml.Style)grid.Resources["TextBoxStyleBig1"];
  54. }
  55. }
  56. }
  57. }

3、演示“ControlTemplate”相关知识点
Controls/UI/ControlTemplate.xaml

  1. <Page
  2. x:Class="Windows10.Controls.UI.ControlTemplate"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.UI"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Name="grid" Background="Transparent">
  11.  
  12. <!--
  13. 注:在 Grid.Resources 指定的资源(支持 ResourceDictionary 方式),其作用域仅在 Grid 之内,全局资源需要在 App.xaml 中配置
  14. -->
  15. <Grid.Resources>
  16.  
  17. <!--
  18. ControlTemplate - 控件模板
  19. x:Key - 标识
  20. TargetType - 目标对象类型
  21. ContentPresenter - 相当于一个容器,用于显示 ContentControl 的 Content 属性指定的内容
  22. TemplateBinding - 模板绑定
  23. -->
  24.  
  25. <ControlTemplate x:Key="ButtonControlTemplate1" TargetType="Button">
  26. <Border BorderBrush="Red" BorderThickness="5">
  27. <!--
  28. TemplateBinding 是一个简单版的 Binding,用于在模板中绑定控件的某个属性,其是 OneWay 的
  29. 那如果想在控件模板中使用双向绑定该怎么做呢,参见“绑定”部分
  30. -->
  31. <Grid Background="{TemplateBinding Background}">
  32. <ContentPresenter HorizontalAlignment="Right" Foreground="Red" />
  33. </Grid>
  34. </Border>
  35. </ControlTemplate>
  36.  
  37. <ControlTemplate x:Key="ButtonControlTemplate2" TargetType="Button">
  38. <Border BorderBrush="Purple" BorderThickness="5">
  39. <Grid Background="{TemplateBinding Background}">
  40. <ContentPresenter HorizontalAlignment="Right" Foreground="Blue" />
  41. </Grid>
  42. </Border>
  43. </ControlTemplate>
  44.  
  45. <!--在 Style 中设置 ControlTemplate-->
  46. <Style x:Key="ButtonStyle" TargetType="Button">
  47. <Setter Property="Template">
  48. <Setter.Value>
  49. <ControlTemplate TargetType="Button">
  50. <Border BorderBrush="Red" BorderThickness="5">
  51. <Grid Background="{TemplateBinding Background}">
  52. <ContentPresenter HorizontalAlignment="Right" Foreground="Green" />
  53. </Grid>
  54. </Border>
  55. </ControlTemplate>
  56. </Setter.Value>
  57. </Setter>
  58. </Style>
  59. </Grid.Resources>
  60.  
  61. <StackPanel Margin="10 0 10 10">
  62.  
  63. <!--指定控件模板-->
  64. <Button Name="button1" Content="我是 Button" Width="300" Margin="5" Background="Yellow" Template="{StaticResource ButtonControlTemplate1}" />
  65.  
  66. <!--动态改变 Control 的控件模板-->
  67. <Button Name="btnChangeControlTemplate" Content="改变控件模板" Margin="5" Click="btnChangeControlTemplate_Click" />
  68.  
  69. <!--在 Style 中设置 ControlTemplate-->
  70. <Button Content="我是 Button" Width="300" Margin="5" Background="Yellow" Style="{StaticResource ButtonStyle}" />
  71.  
  72. <!--内联控件模板-->
  73. <Button Content="我是 Button" Width="300" Margin="5">
  74. <Button.Template>
  75. <ControlTemplate>
  76. <Border BorderBrush="Red" BorderThickness="5">
  77. <Grid Background="Black">
  78. <ContentPresenter HorizontalAlignment="Right" Foreground="Orange" />
  79. </Grid>
  80. </Border>
  81. </ControlTemplate>
  82. </Button.Template>
  83. </Button>
  84.  
  85. </StackPanel>
  86. </Grid>
  87. </Page>

Controls/UI/ControlTemplate.xaml.cs

  1. /*
  2. * 演示“ControlTemplate”相关知识点
  3. *
  4. * 注:
  5. * 1、控件模板是 xaml 语言使用的一种方案,其无法在 c# 中定义
  6. * 2、Template 属性来自 Windows.UI.Xaml.Controls.Control
  7. */
  8.  
  9. using Windows.UI;
  10. using Windows.UI.Xaml;
  11. using Windows.UI.Xaml.Controls;
  12. using Windows.UI.Xaml.Media;
  13.  
  14. namespace Windows10.Controls.UI
  15. {
  16. public sealed partial class ControlTemplate : Page
  17. {
  18. public ControlTemplate()
  19. {
  20. this.InitializeComponent();
  21. }
  22.  
  23. private void btnChangeControlTemplate_Click(object sender, RoutedEventArgs e)
  24. {
  25. // 获取 Application 中的资源
  26. // (Windows.UI.Xaml.Style)Application.Current.Resources["MyControlTemplate"];
  27.  
  28. // 获取关联 xaml 内的资源
  29. if (button1.Template == (Windows.UI.Xaml.Controls.ControlTemplate)grid.Resources["ButtonControlTemplate1"])
  30. {
  31. // 指定控件模板
  32. button1.Template = (Windows.UI.Xaml.Controls.ControlTemplate)grid.Resources["ButtonControlTemplate2"];
  33. }
  34. else
  35. {
  36. button1.Template = (Windows.UI.Xaml.Controls.ControlTemplate)grid.Resources["ButtonControlTemplate1"];
  37. }
  38. }
  39. }
  40. }

控件 UI: 字体的自动继承的特性, Style, ControlTemplate的更多相关文章

  1. 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate

    [源码下载] 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate 作者:webabcd 介绍背水一战 Windows 10 ...

  2. 重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager

    原文:重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState ...

  3. 示例:自定义WPF底层控件UI库 HeBianGu.General.WpfControlLib V2.0版本

    原文:示例:自定义WPF底层控件UI库 HeBianGu.General.WpfControlLib V2.0版本 一.目的:封装了一些控件到自定义的控件库中,方便快速开发 二.实现功能: 基本实现常 ...

  4. 背水一战 Windows 10 (8) - 控件 UI: StateTrigger

    [源码下载] 背水一战 Windows 10 (8) - 控件 UI: StateTrigger 作者:webabcd 介绍背水一战 Windows 10 之 控件 UI VisualState 之 ...

  5. 背水一战 Windows 10 (7) - 控件 UI: VisualState, VisualStateManager, 控件的默认 UI

    [源码下载] 背水一战 Windows 10 (7) - 控件 UI: VisualState, VisualStateManager, 控件的默认 UI 作者:webabcd 介绍背水一战 Wind ...

  6. [原创]C#按比例缩放窗体控件及字体

    按照比例缩放窗体控件及字体,如需等比例缩放,只需将x,y的比例设置成相同即可. 为了减小误差,建议使用原始尺寸来计算比例. private float X, Y; private bool b = f ...

  7. 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding

    原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...

  8. 如何修改全部DevExpress控件的字体

    引用:https://www.devexpress.com/Support/Center/Question/Details/A2761 You can change the default font ...

  9. UIWebView控件中 字体大小和字体样式的修改

    修改UIWebView控件中字体的样式: NSString *htmlString = [NSString stringWithContentsOfFile:self.webPath encoding ...

随机推荐

  1. 转: VMware 安装mac osx 10.11 安装步骤(一)(from伟东)

    http://blog.csdn.net/soachenshui/article/details/49251513

  2. zoj 1610

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  3. Oracle 游标使用全解

    -- 声明游标:CURSOR cursor_name IS select_statement --For 循环游标 --(1)定义游标 --(2)定义游标变量 --(3)使用for循环来使用这个游标 ...

  4. Mysql占用过高CPU时的优化手段

    Mysql占用CPU过高的时候,该从哪些方面下手进行优化?占用CPU过高,可以做如下考虑:1)一般来讲,排除高并发的因素,还是要找到导致你CPU过高的哪几条在执行的SQL,show processli ...

  5. python实现一个图灵机器人

    这标题就是个噱头...其实用的别人的接口,就是这货. 下面是代码: # -*- coding: utf-8 -*- import urllib,urllib2 import sys import js ...

  6. 安装ESXi5.5遇到Relocating modules and starting up the kernel的处理

    在一些Dell较旧的服务器上安装ESXi 5.x时, 会遇到卡在Relocating modules and starting up the kernel过不去的问题. 比如我装的这台CS24VSS. ...

  7. 动画制作库tween样例学习

    mark: https://www.npmjs.com/package/tween

  8. topshelf包装redis为windows服务

    topshelf包装redis为windows服务 Redis服务端目前用的是控制台程序运行,部署的时候能作为windows服务后台运行感觉更好.找到一篇文章Running Redis as a Wi ...

  9. JS使构造函数与new操作符无关

    function User(name, passwordHash) { this.name = name; this.passwordHash = passwordHash; } 当使用User函数创 ...

  10. java中String、StringBuffer、StringBuilder的区别

    java中String.StringBuffer.StringBuilder是编程中经常使用的字符串类,他们之间的区别也是经常在面试中会问到的问题.现在总结一下,看看他们的不同与相同. 1.可变与不可 ...