背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
作者:webabcd
介绍
背水一战 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);
}
}
}
OK
[源码下载]
背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定的更多相关文章
- 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合
[源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...
- 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧
[源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...
- 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换
[源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...
- 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue
[源码下载] 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue 作者:weba ...
- 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null
[源码下载] 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null 作者:webabcd 介绍背水一战 Windows 10 之 XAML ...
- 背水一战 Windows 10 (8) - 控件 UI: StateTrigger
[源码下载] 背水一战 Windows 10 (8) - 控件 UI: StateTrigger 作者:webabcd 介绍背水一战 Windows 10 之 控件 UI VisualState 之 ...
- 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate
[源码下载] 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate 作者:webabcd 介绍背水一战 Windows 10 ...
- 背水一战 Windows 10 (78) - 自定义控件: 基础知识, 依赖属性, 附加属性
[源码下载] 背水一战 Windows 10 (78) - 自定义控件: 基础知识, 依赖属性, 附加属性 作者:webabcd 介绍背水一战 Windows 10 之 控件(自定义控件) 自定义控件 ...
- 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout
[源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...
随机推荐
- Android学习——windows下搭建Cygwin环境
在上一篇博文<Android学习——windows下搭建NDK_r9环境>中,我们详细的讲解了在windows下进行Android NDK开发环境的配置,我们也讲到了在NDk r7以后,我 ...
- Module Zero之权限管理
返回<Module Zero学习目录> 概览介绍 角色权限 用户权限 概览介绍 Module-Zero实现了ABP授权系统的IPermissionChecker接口.这篇文章中,我们将会看 ...
- Angular1还是Angular2
Angular1还是Angular2 学完angular1,尝试去看了下angular2,虽然号称更强更快,可是这基于ES6和TypeScript的全新框架让人完全招架不住,而且我只是需要angula ...
- YY一下淘宝商品模型
淘宝的电商产品种类非常丰富,必然得力于其商品模型的高度通用性和扩展性. 下面我将亲自操作淘宝商品的发布过程,结合网上其他博客对淘宝网商品库的分析,简单谈谈我的理解. 注:下面不特殊说明,各个表除主键外 ...
- JS原生第七篇 (帅哥)
1.1 复习 offset 自己的 偏移 offsetWidth 得到自己的宽度 offsetHeight 构成 : width + padding + border div ...
- The replication agent has not logged a progress message in 10 minutes.
打开Replication Monitor,在Subscription Watch List Tab中,发现有大量的status= “Performance critical” 的黄色Warning, ...
- C#设计模式系列:简单工厂模式(Simple Factory)
1. 简单工厂模式简介 1.1 定义 简单工厂模式定义一个Factory类,可以根据参数的不同返回不同类的实例,被创建的实例通常有共同的父类. 简单工厂模式只需要一个Factory类. 简单工厂模式又 ...
- 利用typescript使backbone强类型智能提示
模型类一旦多了没有强类型和智能提示是相当痛苦的,所以. 仅仅用ts定义一个模型类: class Person extends Backbone.Model { defaults = { Name:&q ...
- XenServer pool 移除server 设置master
如果因为Pool中Master主机由于某种原因导致失效,会引起整个Pool进入紧急模式,恢复步骤如下: 在成员服务器上输入如下命令 # xe host-emergency-ha-disable ...
- JavaScript 中的类方法,对象方法,Prototype方法
<script type="text/javascript"> function baseClass() { this.showMsg = function() { a ...