[源码下载]

背水一战 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 绑定的更多相关文章

  1. 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合

    [源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...

  2. 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧

    [源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...

  3. 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换

    [源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...

  4. 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue

    [源码下载] 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue 作者:weba ...

  5. 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null

    [源码下载] 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null 作者:webabcd 介绍背水一战 Windows 10 之 XAML ...

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

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

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

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

  8. 背水一战 Windows 10 (78) - 自定义控件: 基础知识, 依赖属性, 附加属性

    [源码下载] 背水一战 Windows 10 (78) - 自定义控件: 基础知识, 依赖属性, 附加属性 作者:webabcd 介绍背水一战 Windows 10 之 控件(自定义控件) 自定义控件 ...

  9. 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout

    [源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...

随机推荐

  1. (DNS被劫持所导致的)QQ音乐与视频网页打开很慢的解决方法

    这周开始发现一个很让人抓狂的现象,QQ音乐网页(http://y.qq.com)与QQ视频(http://v.qq.com/)网页打开超慢,甚至是无法打开,严重影响了业余的音乐视频生活. 以QQ视频为 ...

  2. MySQL 存储过程

    MySQL 存储过程 存储过程是通过给定的语法格式编写自定义的数据库API,类似于给数据库编写可执行函数. 简介 存储过程是一组为了完成特定功能的SQL语句集合,是经过编译后存储在数据库中. 存储过程 ...

  3. ASP.NET MVC 路由(二)

     ASP.NET MVC路由(二) 前言 在上一篇中,提及了Route.RouteCollection对象的一些信息,以及它们的结构所对应的关系.按照处理流程走下来还有遗留的疑问没有解决这个篇幅就来讲 ...

  4. ystep jQuery流程、步骤插件

    今天小菜给大家带来又一款给力jQuery插件:ystep. 从名称上大致可以看出,这是一个流程步骤插件. 如果无意外的话,这可能是小菜近期最后一个作品了...苦逼的小菜即将创业,可能就没时间折腾啦~好 ...

  5. android内部培训视频_第一节

    声明:本视频为公司内部做android培训时录制的,无任何商业目的.同时鉴于水平有限,可能不符合您的需求,放在这里的目的是提供给公司同事下载,作为培训的一个记录,也作为一个系列教程的自我督促完成的理由 ...

  6. Python----reduce原来是这样用的

    官方解释: Apply function of two arguments cumulatively to the items of iterable, from left to right, so ...

  7. 翻译-使用Ratpack和Spring Boot打造高性能的JVM微服务应用

    这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices with Ratpack & Spring Boot,InfoQ上的中 ...

  8. Gyp语法规则参考 & 工具的使用

    转自:http://www.cnblogs.com/nanvann/p/3913880.html 翻译自 https://code.google.com/p/gyp/wiki/GypLanguageS ...

  9. Report processing of Microsoft Dynamic AX

    Report processing of Microsoft Dynamic AX 版权声明:本文为博主原创文章,未经博主允许不得转载. The implementation of a general ...

  10. Java 对象引用方式 —— 强引用、软引用、弱引用和虚引用

    Java中负责内存回收的是JVM.通过JVM回收内存,我们不需要像使用C语音开发那样操心内存的使用,但是正因为不用操心内存的时候,也会导致在内存回收方面存在不够灵活的问题.为了解决内存操作不灵活的问题 ...