字体的自动继承的特性

  • Style 样式
  • ControlTemplate 控件模板

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

<Page
x:Class="Windows10.Controls.UI.FontInherit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" FontSize="100"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <!--
本例演示字体的自动继承的特性
Font 相关的设置来自 Windows.UI.Xaml.Controls.Control
--> <!--
继承了 Page 的关于 Font 的设置
-->
<TextBlock Text="FontSize = 100" /> <UserControl FontSize="50">
<!--
继承了 UserControl 的关于 Font 的设置
-->
<TextBlock Text="FontSize = 50" />
</UserControl> </StackPanel>
</Grid>
</Page>

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

<Page
x:Class="Windows10.Controls.UI.Style"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Name="grid" Background="Transparent"> <!--
注:在 Grid.Resources 指定的资源(支持 ResourceDictionary 方式),其作用域仅在 Grid 之内,全局资源需要在 App.xaml 中配置
-->
<Grid.Resources> <!--
Style - 样式
x:Key - 标识(不指定此值,则样式会应用于所有 TargetType 所指定的类型)
TargetType - 目标对象类型
BasedOn - 指定当前样式的父样式(此样式会继承指定的父样式)
Setter - 属性设置器
Property - 需要设置的属性名称
Value - 需要设置的属性值
--> <!--
自定义一个基础样式
-->
<Style x:Key="TextBoxStyleBase" TargetType="TextBox">
<Setter Property="Foreground" Value="Red"/>
</Style> <!--
这是自定义了全局样式,但是其他的自定义样式并不会自动继承这个自定义全局样式
所以,此处用 BasedOn 继承基础样式,然后其他自定义样式也继承基础样式就好(这就相当于继承了这个自定义全局样式)
-->
<Style TargetType="TextBox" BasedOn="{StaticResource TextBoxStyleBase}"> </Style> <!--
不会自动继承上面的自定义全局样式
-->
<Style x:Key="TextBoxStyleBig1" TargetType="TextBox">
<Setter Property="FontSize" Value="24"/>
<Setter Property="Height" Value="60"/>
</Style> <!--
继承了基础样式(相当于继承了上面的自定义全局样式)
-->
<Style x:Key="TextBoxStyleBig2" TargetType="TextBox" BasedOn="{StaticResource TextBoxStyleBase}">
<Setter Property="FontSize" Value="24"/>
<Setter Property="Height" Value="60"/>
</Style> </Grid.Resources> <StackPanel Margin="10 0 10 10"> <!--默认使用自定义全局样式-->
<TextBox Name="textBox1" Text="我是 TextBox" Margin="5" /> <!--指定样式资源-->
<TextBox Name="textBox2" Text="我是 TextBox" Margin="5" Style="{StaticResource TextBoxStyleBig1}" /> <!--动态改变 FrameworkElement 的样式-->
<Button Name="btnChangeStyle" Margin="5" Content="改变样式" Click="btnChangeStyle_Click" /> <!--内联样式-->
<TextBox Name="textBox3" Text="我是 TextBox" Margin="5">
<ToolTipService.ToolTip>
<ToolTip Name="toolTip" Content="ToolTipService.ToolTip" Placement="Bottom">
<ToolTip.Style>
<Style TargetType="ToolTip">
<Setter Property="Foreground" Value="Blue" />
</Style>
</ToolTip.Style>
</ToolTip>
</ToolTipService.ToolTip>
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Background" Value="Green"/>
</Style>
</TextBox.Style>
</TextBox> <!--在 c# 中定义样式-->
<TextBox Name="textBox4" Text="我是 TextBox" Margin="5" /> </StackPanel>
</Grid>
</Page>

Controls/UI/Style.xaml.cs

/*
* 演示“Style”相关知识点
*
* 注:
* 1、Style 属性来自 Windows.UI.Xaml.FrameworkElement
*/ using System;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Controls.UI
{
public sealed partial class Style : Page
{
public Style()
{
this.InitializeComponent(); this.Loaded += Style_Loaded;
} // 在 c# 中定义样式
private void Style_Loaded(object sender, RoutedEventArgs e)
{
Windows.UI.Xaml.Style style = new Windows.UI.Xaml.Style();
style.TargetType = typeof(TextBox); Setter setter1 = new Setter();
setter1.Property = TextBox.BackgroundProperty;
setter1.Value = Colors.Blue; style.Setters.Add(setter1); textBox4.Style = style;
} // 改变样式
private void btnChangeStyle_Click(object sender, RoutedEventArgs e)
{
// 获取 Application 中的资源
// (Windows.UI.Xaml.Style)Application.Current.Resources["myStyle"]; // 获取关联 xaml 内的资源
if (textBox2.Style == (Windows.UI.Xaml.Style)grid.Resources["TextBoxStyleBig1"])
{
// 指定样式
textBox2.Style = (Windows.UI.Xaml.Style)grid.Resources["TextBoxStyleBig2"];
}
else
{
textBox2.Style = (Windows.UI.Xaml.Style)grid.Resources["TextBoxStyleBig1"];
}
}
}
}

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

<Page
x:Class="Windows10.Controls.UI.ControlTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Name="grid" Background="Transparent"> <!--
注:在 Grid.Resources 指定的资源(支持 ResourceDictionary 方式),其作用域仅在 Grid 之内,全局资源需要在 App.xaml 中配置
-->
<Grid.Resources> <!--
ControlTemplate - 控件模板
x:Key - 标识
TargetType - 目标对象类型
ContentPresenter - 相当于一个容器,用于显示 ContentControl 的 Content 属性指定的内容
TemplateBinding - 模板绑定
--> <ControlTemplate x:Key="ButtonControlTemplate1" TargetType="Button">
<Border BorderBrush="Red" BorderThickness="5">
<!--
TemplateBinding 是一个简单版的 Binding,用于在模板中绑定控件的某个属性,其是 OneWay 的
那如果想在控件模板中使用双向绑定该怎么做呢,参见“绑定”部分
-->
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Right" Foreground="Red" />
</Grid>
</Border>
</ControlTemplate> <ControlTemplate x:Key="ButtonControlTemplate2" TargetType="Button">
<Border BorderBrush="Purple" BorderThickness="5">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Right" Foreground="Blue" />
</Grid>
</Border>
</ControlTemplate> <!--在 Style 中设置 ControlTemplate-->
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderBrush="Red" BorderThickness="5">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Right" Foreground="Green" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources> <StackPanel Margin="10 0 10 10"> <!--指定控件模板-->
<Button Name="button1" Content="我是 Button" Width="300" Margin="5" Background="Yellow" Template="{StaticResource ButtonControlTemplate1}" /> <!--动态改变 Control 的控件模板-->
<Button Name="btnChangeControlTemplate" Content="改变控件模板" Margin="5" Click="btnChangeControlTemplate_Click" /> <!--在 Style 中设置 ControlTemplate-->
<Button Content="我是 Button" Width="300" Margin="5" Background="Yellow" Style="{StaticResource ButtonStyle}" /> <!--内联控件模板-->
<Button Content="我是 Button" Width="300" Margin="5">
<Button.Template>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="5">
<Grid Background="Black">
<ContentPresenter HorizontalAlignment="Right" Foreground="Orange" />
</Grid>
</Border>
</ControlTemplate>
</Button.Template>
</Button> </StackPanel>
</Grid>
</Page>

Controls/UI/ControlTemplate.xaml.cs

/*
* 演示“ControlTemplate”相关知识点
*
* 注:
* 1、控件模板是 xaml 语言使用的一种方案,其无法在 c# 中定义
* 2、Template 属性来自 Windows.UI.Xaml.Controls.Control
*/ using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media; namespace Windows10.Controls.UI
{
public sealed partial class ControlTemplate : Page
{
public ControlTemplate()
{
this.InitializeComponent();
} private void btnChangeControlTemplate_Click(object sender, RoutedEventArgs e)
{
// 获取 Application 中的资源
// (Windows.UI.Xaml.Style)Application.Current.Resources["MyControlTemplate"]; // 获取关联 xaml 内的资源
if (button1.Template == (Windows.UI.Xaml.Controls.ControlTemplate)grid.Resources["ButtonControlTemplate1"])
{
// 指定控件模板
button1.Template = (Windows.UI.Xaml.Controls.ControlTemplate)grid.Resources["ButtonControlTemplate2"];
}
else
{
button1.Template = (Windows.UI.Xaml.Controls.ControlTemplate)grid.Resources["ButtonControlTemplate1"];
}
}
}
}

控件 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. 多年前写的一个ASP.NET网站管理系统,到现在有些公司在用

    多年前写的一个ASP.NET网站管理系统,到现在有些公司在用 今早上接到一个电话,自已多年前写的一个ASP.NET网站管理系统,一个公司在用,出了点问题, 第一点是惊奇,5,6年前的东东,手机号码换了 ...

  2. 有关数据库行、锁 的几个问题(rowlock)

    行锁的基本说明: SELECT au_lname FROM authors WITH (NOLOCK) 锁定提示                                 描述  HOLDLOC ...

  3. NOIP2015提高组Day1 Message

    题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...

  4. javascript中比较数字大小

    做项目,遇到一个让人非常纠结的问题,就是获取的两个值比较,却出现了一位数比二位数大的情况.刚开始还以为哪里写错了,检查了几遍,用ie调了下,意识到是应该是用错了比较方法了.才想起以前也碰到过这种情况的 ...

  5. Zygote浅谈

    Zygote是什么 操作系统中,进程实际上是文件到地址空间的映射像.进程将要运行时,由操作系统将其映射到地址空间,完成这项工作的事物本质也应是一个进程,我们称这个进程为孵化进程,那么这个进程怎么收到消 ...

  6. java 接口(上)

    1.接口中的方法都是抽象方法.而普通的抽象类里面不一定都是抽象方法.抽象类中必须有抽象方法,同时也可以有非抽象方法.继承抽象父类的子类中,如果依然有抽象方法,那么这个子类也是抽象类.即只要类中有抽象方 ...

  7. windows下安装memcache的基本步骤

    本文主要解决的是window下memcached的安装的问题,在使用的过程中经常会被第一步环境的配置搞混,本文结合我的配置过程和遇到的问题,做一个总结 1,开启php memcache的扩展,在文件  ...

  8. DBA必备:MySQL数据库常用操作和技巧

    DBA必备:MySQL数据库常用操作和技巧 2011-02-25 15:31 kaduo it168 字号:T | T MySQL数据库可以说是DBA们最常见和常用的数据库之一,为了方便大家使用,老M ...

  9. NET Core项目定义Item Template

    NET Core项目定义Item Template 作为这个星球上最强大的IDE,Visual Studio不仅仅提供了很多原生的特性,更重要的是它是一个可定制的IDE,比如自定义Project Te ...

  10. Spring Security笔记:登录尝试次数限制

    今天在前面一节的基础之上,再增加一点新内容,默认情况下Spring Security不会对登录错误的尝试次数做限制,也就是说允许暴力尝试,这显然不够安全,下面的内容将带着大家一起学习如何限制登录尝试次 ...