今天看到这一句代码时候,自己只是知道绑定了,可是不知道绑定了什么啊 
就去查了一下,后来说的好像是绑定的TemplateParent返回的 一个值。可是这是为什么呢, 
有的说是绑定的是一个资源。 
下面有一个例子 
下面的示例显示名为 NumericUpDown 的自定义控件的 Style 定义。将 TextBlock 的 Text 属性绑定到对象 TemplatedParent 的 Value,在此情况下即应用了此 Style 的 NumericUpDown 控件。

<!--ControlTemplate for NumericUpDown that inherits from
Control.-->
<Style TargetType="{x:Type local:NumericUpDown}">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:NumericUpDown}">
<Grid Margin="3">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions> <Border BorderThickness="1" BorderBrush="Gray"
Margin="2" Grid.RowSpan="2"
VerticalAlignment="Center" HorizontalAlignment="Stretch"> <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}"
Width="60" TextAlignment="Right" Padding="5"/>
</Border> <RepeatButton Command="{x:Static local:NumericUpDown.IncreaseCommand}"
Grid.Column="1" Grid.Row="0">Up</RepeatButton> <RepeatButton Command="{x:Static local:NumericUpDown.DecreaseCommand}"
Grid.Column="1" Grid.Row="1">Down</RepeatButton> </Grid> </ControlTemplate>
</Setter.Value>
</Setter>
</Style>

TemplateBindingExtension 类 
实现一个标记扩展,该标记扩展支持在模板中属性的值和模板化控件上某个其他公开的属性的值之间进行绑定。 
下面的示例演示 ControlTemplate,它定义一个水平放置且具有圆角的 ListBox。TemplateBinding 指定 Border 的 Background 应与 ListBox 上设置的 Background 值同步。如果想要使控件用户能够控制某些属性的值,可以在 ControlTemplate 中使用 TemplateBinding

C#

<Style TargetType="ListBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Border CornerRadius="5" Background="{TemplateBinding ListBox.Background}">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<StackPanel Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center"
IsItemsHost="True"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

下面的示例演示 Label 控件的 ControlTemplate。HorizontalAlignment 和 VerticalAlignment 属性绑定到此 ControlTemplate 所应用于的 Label 控件的 HorizontalContentAlignment 和 VerticalContentAlignment 属性的值。

C#

<Style x:Key="{x:Type Label}" TargetType="Label">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border>
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground"
Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

RelativeSource.TemplatedParent 属性wpf的更多相关文章

  1. [WPF系列-高级TemplateBinding vs RelativeSource TemplatedParent]

    What is the difference between these 2 bindings: <ControlTemplate TargetType="{x:Type Button ...

  2. RelativeSource={RelativeSource TemplatedParent}

    <!--按钮样式开始-->     <Style x:Key="NotifyBtnStyle" TargetType="{x:Type commondC ...

  3. Wpf 数据绑定简介、实例1

    简介:1.WPF绑定使用的源属性必须是依赖项属性,这是因为依赖项属性具有内置的更改通知支持,元素绑定表达式使用了Xaml扩展标记, WPF绑定一个控件是使用Binding.ElementName, 绑 ...

  4. WPF教程(四)RelativeSource属性

    我们进行Bingding时,如果明确知道数据源的Name,就能用Source或者ElementName进行绑定,但是有时候我们需要绑定的数据源可能没有明确的Name,此时我们就需要利用Bingding ...

  5. 在WPF中如何使用RelativeSource绑定

    在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel ...

  6. 正确理解WPF中的TemplatedParent

    (注:Logical Tree中文称为逻辑树,Visual Tree中文称为可视化树或者视觉树,由于名称不是很统一,文中统一用英文名称代表两个概念,况且VisualTreeHelper和Logical ...

  7. wpf ComboBox的SelectionBoxItem相关依赖属性

    以前没有注意SelectionBoxItem相关依赖属性,这几天看wpf源码 特意研究了一番 <Style x:Key="ComboBoxStyle1" TargetType ...

  8. wpf控件开发基础(5) -依赖属性实践

    原文:wpf控件开发基础(5) -依赖属性实践 知识回顾 接上篇,回顾这三篇讲了什么东西 首先说明了属性的现存问题,然后介绍了依赖属性的基本用法及其解决方案,由于依赖属性以静态属性的方式存在,进而又介 ...

  9. WPF依赖属性的正确学习方法

    前言 我在学习WPF的早期,对依赖属性理解一直都非常的不到位,其恶果就是,我每次在写依赖属性的时候,需要翻过去的代码来复制黏贴. 相信很多朋友有着和我相同的经历,所以这篇文章希望能帮助到那些刚刚开始学 ...

随机推荐

  1. vue - node_modules

    详情见:node_modules导包机制 在打包或者结束项目时,这个文件夹(node_modules)不应该被打包. 你应该打包其它的文件,如果要运行(直接用以下命令安装即可,它会根据package. ...

  2. 弹出键盘windowsoftinputmode属性设置值

    windowSoftInputMode属性设置值 2012-08-30 16:49 1592人阅读 评论(0) 收藏 举报 androidattributes活动 (1).AndroidManifes ...

  3. FreeMarker静态化文件解决SEO推广问题

    1.问题背景 SEO一直是站点对外推广的一个重要手段,如何可以让搜索引擎高速搜索到站点对于增强站点的浏量,提升站点对外形象有着重要意义.那么如何可以对SEO进行优化呢?一个很经常使用的手段就是在网页的 ...

  4. 详解Struts1中的struts-config.xml配置文件【一】

    搞清楚struts-config.xml中各项元素的作用,对于我们构建web项目有莫大的好处.<struts-config>是struts的根元素,它主要有8个子元素,DTD定义如下: & ...

  5. Spring技术内幕:Spring AOP的实现原理(三)

    生成SingleTon代理对象在getSingleTonInstance方法中完毕,这种方法时ProxyFactoryBean生成AopProxy对象的入口.代理对象会封装对target目标对象的调用 ...

  6. 大话Web-Audio-Api

    大话Web-Audio-Api 转:https://www.jianshu.com/p/0079d1fe7496 简单的例子: <script> var context; var musi ...

  7. Spark-shell 启动WARN---java.net.BindException: Address already in use

    同时打开了两个SecureCRT的终端界面,其中一个已经进入了Spark-shell,在另一个SecureCRT界面中执行 "spark-shell --master yarn --depl ...

  8. JS 16进制加密解密

    http://www.zc520.cc/js/62.html <script type="text/javascript"> function JavaDe(){ va ...

  9. JS高程3:表单脚本

    HTML和CSS对表单的操作还是比较乏力的,在表单操作中,JS势必会使用到. 基础知识 文本框 选择框 序列化 富文本编辑器 基础知识 HTMLFormElement接口可以创建或者修改<for ...

  10. pair + map 函数结合使用

    题目链接:codeforces 44A5birch yellowmaple redbirch yellowmaple yellowmaple green 4 3oak yellowoak yellow ...