绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
介绍
背水一战 Windows 10 之 绑定
- TemplateBinding 绑定
- 与 RelativeSource 绑定
- 与 StaticResource 绑定
示例
1、演示 TemplateBinding 的用法
Bind/TemplateBindingDemo.xaml

<Page
x:Class="Windows10.Bind.TemplateBindingDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <!--
演示 TemplateBinding 的用法
TemplateBinding 是一个简单版的 Binding,用于在 ControlTemplate 中做属性之间的绑定(如果需要 Binding 的其他特性该怎么做呢?参见 BindingRelativeSource.xaml)
--> <StackPanel.Resources>
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<StackPanel>
<!--
ContentPresenter 的 Width 绑定 Button 的 Width
ContentPresenter 的 Height 绑定 Button 的 Width
-->
<ContentPresenter HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="White" Background="Orange"
Width="{TemplateBinding Width}" Height="{TemplateBinding Width}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Resources> <Button Content="我是 Button" Width="128" Style="{StaticResource ButtonStyle}" /> </StackPanel>
</Grid>
</Page>

2、演示 Binding 中的一个扩展标记 RelativeSource 的应用
Bind/BindingRelativeSource.xaml

<Page
x:Class="Windows10.Bind.BindingRelativeSource"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <!--
演示 Binding 中的一个扩展标记 RelativeSource 的应用,其用于指定关联数据源为 Self 或 TemplatedParent
--> <!--
RelativeSource={RelativeSource TemplatedParent} - 仅在 ControlTemplate 中适用,用于指定数据源来自引用了该 ControlTemplate 的 Control
-->
<StackPanel.Resources>
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<StackPanel>
<ContentPresenter Foreground="White" />
<!--
TemplateBinding 是一个简单版的 Binding,他是 OneWay 的 如果在设计 ControlTemplate 时需要 Binding 的其他特性(比如我想要 TwoWay 的模式)该怎么办呢?
那就需要通过 Binding 来做绑定(这样就可以使用 Binding 的各种特性了),然后通过 RelativeSource={RelativeSource TemplatedParent} 来指定数据源来自引用了该 ControlTemplate 的 Control
-->
<Slider Minimum="1" Maximum="100" Foreground="White" IsThumbToolTipEnabled="False"
Width="{TemplateBinding Width}" Value="{Binding Content, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Resources> <Button Width="300" Content="50" Style="{StaticResource ButtonStyle}" Margin="5" /> <!--
RelativeSource={RelativeSource Self} - 指定数据源为自己本身
-->
<TextBlock Text="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" Tag="webabcd" Margin="5" /> </StackPanel>
</Grid>
</Page>

3、演示如何与 StaticResource 绑定
Bind/BindingStaticResource.xaml

<Page
x:Class="Windows10.Bind.BindingStaticResource"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10" x:Name="panel"> <!--
演示如何与 StaticResource 绑定
关于 StaticResource 的说明请参见:/Resource/StaticResourceDemo.xaml
--> <StackPanel.Resources> <x:Double x:Key="TextFontSize">32</x:Double>
<SolidColorBrush x:Key="TextForeground" Color="#00FF00" /> <Style x:Key="MyStyle" TargetType="TextBox">
<!--绑定 StaticResource 资源-->
<Setter Property="FontSize" Value="{Binding Source={StaticResource TextFontSize}}"/>
<!--绑定 StaticResource 资源的简化写法-->
<Setter Property="Foreground" Value="{StaticResource TextForeground}"/>
</Style> </StackPanel.Resources> <!--绑定 StaticResource 资源-->
<TextBox Text="我是TextBox" Style="{Binding Source={StaticResource MyStyle}}" Margin="5" /> <!--绑定 StaticResource 资源的简化写法-->
<TextBox Text="我是TextBox" Style="{StaticResource MyStyle}" Margin="5" /> <!--演示如何在 C# 端绑定 StaticResource-->
<TextBox Name="textBox" Text="我是TextBox" Margin="5" /> </StackPanel>
</Grid>
</Page>

Bind/BindingStaticResource.xaml.cs

/*
* 演示如何与 StaticResource 绑定(关于 StaticResource 的说明请参见:/Resource/StaticResourceDemo.xaml)
*/ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data; namespace Windows10.Bind
{
public sealed partial class BindingStaticResource : Page
{
public BindingStaticResource()
{
this.InitializeComponent(); this.Loaded += BindingStaticResource_Loaded;
} // 在 C# 端绑定 StaticResource
private void BindingStaticResource_Loaded(object sender, RoutedEventArgs e)
{
// 实例化 Binding 对象
Binding binding = new Binding()
{
Source = panel.Resources["MyStyle"]
}; // 将目标对象的目标属性与指定的 Binding 对象关联
BindingOperations.SetBinding(textBox, TextBox.StyleProperty, binding);
}
}
}
绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定的更多相关文章
- 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
[源码下载] 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定 作者:we ...
- 使用jquery移除前面通过onclick绑定的元素的事件,然后重新绑定别的函数来执行onclick事件。
http://caibaojian.com/css3/experience/bugs.htm 使用jquery移除前面通过onclick绑定的元素的事件,然后重新绑定别的函数来执行onclick事件. ...
- jQuery绑定和解绑点击事件及重复绑定解决办法
原文地址:http://www.111cn.net/wy/jquery/47597.htm 绑点击事件这个是jquery一个常用的功能,如click,unbind等等这些事件绑定事情,但还有很多朋友不 ...
- Silverlight中的TabControl如何绑定数据?重写tabcontrol和tabItem 解决绑定友好问题。可以绑定对象集合
在 WPF 中,TabControl 可以直接将 ItemsSource 绑定数据源,见 将 TabControl 绑定到数据的示例 http://msdn.microsoft.com/zh-cn/l ...
- SpringMVC(六):@RequestMapping下使用@RequestHeader绑定请求报头的属性值、@CookieValue绑定请求中的Cookie值
备注:我本地浏览器的报头(Request Header)信息如下: Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image ...
- 如何理解一台服务器可以绑定多个ip,一个ip可以绑定多个域名
一个域名只能对应一个IP的意思是域名在DNS服务器里做解析的时候 一条记录只能指向一个IP地址.这个是死规定,试想一下,如果一个子域名指向了2个ip ,当访问者打开这个域名的时候,浏览器是展示哪个IP ...
- javascript事件委托和jQuery事件绑定on、off 和one以及on绑定多个事件(重要)
一. 事件委托什么是事件委托?用现实中的理解就是:有100 个学生同时在某天中午收到快递,但这100 个学生不可能同时站在学校门口等,那么都会委托门卫去收取,然后再逐个交给学生.而在jQuery 中, ...
- vue中的绑定class和微信小程序中的绑定class的区别
微信小程序 小程序里面的class与style绑定,遵循HTML特性绑定,有关于HTML绑定.在进行class与style绑定时,可以直接绑定,也可以带上逻辑与,或者三元运算进行条件控制 JS dat ...
- WPF DevExpress Chart控件 界面绑定数据源,不通过C#代码进行绑定
<Grid x:Name="myGrid" Loaded="Grid_Loaded" DataContext="{Binding PartOne ...
随机推荐
- Extjs API - JS Duck
1. 安裝JS Duck3 tar.gz版本 https://nodeload.github.com/senchalabs/jsduck/tarball/master exe版本 http://c ...
- python画柱状图并且输出到html文件
import matplotlibmatplotlib.use('Agg')import matplotlib.pyplot as pltfrom Cstring import StringIO y ...
- PAT 1020. 月饼 (25)
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...
- MVC HTTP 错误 403.14 - Forbidden
HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容. 最可能的原因: 没有为请求的 URL 配置默认文档,并且没有在服务器上启用目录浏览. 可尝试的操作: ...
- [转]IIS添加MIME扩展类型及常用的MIME类型列表
http://www.cr173.com/html/18997_1.html 经常我在用IIS做为下载服务器的时候有时传上去的文件比如 xxx.iso 文件名名是传上去了,但是用http打开的时候确显 ...
- Linux下Perl的安装(转)
原文地址:Linux下Perl的安装 今天在虚拟机测试shell脚本的时候,有些命令使用不了. 比如说 mysqlhotcopy ,它提示Perl的版本太低. 我用的 RedHat9 的Perl才5. ...
- 实验三 敏捷开发与XP实践
实验内容 1. XP基础 2. XP核心实践 3. 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2.完成实验 ...
- 谈谈软件项目的dependency
说到软件项目的依赖管理,可以从三个方面来考虑: 一.由build system控制的dependency 现在的build system,都支持一定程度上的dependency management, ...
- MFC下debug改成release版本出现问题及解决办法
自己在debug下成功运行了自己写的测试自己写第三方库的程序,这里有用到opencv库,所以同时用到了自己的库和opencv的库,需求因为要进行速度的测试,是想要把debug改成release版本,这 ...
- 海王星给你好看!FineUI v4.0公测版发布暨《你找BUG我送书》活动开始(活动已结束!)
<FineUI v4.0 你找BUG我送书>活动已结束,恭喜如下三位网友获得由 FineUI 作者亲自翻译的图书<jQuery实战 第二版>! 奋斗~ 吉吉﹑ purplebo ...