RelativeSource.TemplatedParent 属性wpf
今天看到这一句代码时候,自己只是知道绑定了,可是不知道绑定了什么啊
就去查了一下,后来说的好像是绑定的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的更多相关文章
- [WPF系列-高级TemplateBinding vs RelativeSource TemplatedParent]
What is the difference between these 2 bindings: <ControlTemplate TargetType="{x:Type Button ...
- RelativeSource={RelativeSource TemplatedParent}
<!--按钮样式开始--> <Style x:Key="NotifyBtnStyle" TargetType="{x:Type commondC ...
- Wpf 数据绑定简介、实例1
简介:1.WPF绑定使用的源属性必须是依赖项属性,这是因为依赖项属性具有内置的更改通知支持,元素绑定表达式使用了Xaml扩展标记, WPF绑定一个控件是使用Binding.ElementName, 绑 ...
- WPF教程(四)RelativeSource属性
我们进行Bingding时,如果明确知道数据源的Name,就能用Source或者ElementName进行绑定,但是有时候我们需要绑定的数据源可能没有明确的Name,此时我们就需要利用Bingding ...
- 在WPF中如何使用RelativeSource绑定
在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel ...
- 正确理解WPF中的TemplatedParent
(注:Logical Tree中文称为逻辑树,Visual Tree中文称为可视化树或者视觉树,由于名称不是很统一,文中统一用英文名称代表两个概念,况且VisualTreeHelper和Logical ...
- wpf ComboBox的SelectionBoxItem相关依赖属性
以前没有注意SelectionBoxItem相关依赖属性,这几天看wpf源码 特意研究了一番 <Style x:Key="ComboBoxStyle1" TargetType ...
- wpf控件开发基础(5) -依赖属性实践
原文:wpf控件开发基础(5) -依赖属性实践 知识回顾 接上篇,回顾这三篇讲了什么东西 首先说明了属性的现存问题,然后介绍了依赖属性的基本用法及其解决方案,由于依赖属性以静态属性的方式存在,进而又介 ...
- WPF依赖属性的正确学习方法
前言 我在学习WPF的早期,对依赖属性理解一直都非常的不到位,其恶果就是,我每次在写依赖属性的时候,需要翻过去的代码来复制黏贴. 相信很多朋友有着和我相同的经历,所以这篇文章希望能帮助到那些刚刚开始学 ...
随机推荐
- npm技巧
你看一个同事正在编码,其中应用上了一些简写和技巧,不知为何,你不熟悉它并且你的大脑一片混乱,这种场景发生在过去我们每个人身上. 在这篇短文中,我们将会介绍一些非常实用的 npm 技巧.有很多文章中的技 ...
- AppIcon尺寸
- python安装libxml2和pyquery
安装.net framework 4.5.2 https://www.microsoft.com/zh-CN/download/details.aspx?id=42641 安装C编译器 python2 ...
- Zoho CEO:云计算泡沫巨大 Salesforce仅仅是新的Siebel
最近Zoho CEO - Sridhar Vembu接受科技博客媒体Diginomica的专訪,从独特的眼光和见解.讲述了云计算行业环境.SaaS公司的生存状态.商业观念以及Zoho的商业模式. Sr ...
- CSS总结:
给一个div加上边框,为了让他能看到边框,给他加上颜色 border: 2px solid; border-color: #00a1e9; solid表示实线边框.没有这个看不到边框. {border ...
- expect脚本免密码
#!/usr/bin/expect set timeout spawn ssh root@20.0.102.19 expect "password:" send "123 ...
- FIS3项目构建
概述 FIS3采取了类似CSS语法一样的配置风格,易于理解与上手.FIS3 是面向前端的工程构建工具.解决前端工程中性能优化.资源加载(异步.同步.按需.预加载.依赖管理.合并.内嵌).模块化开发.自 ...
- Javascript 判断网页横竖屏
本篇文章由:http://xinpure.com/javascript-to-determine-page-anyway-screen/ Html5 流行至今,自适应的网站已经多如牛毛,但是横竖屏的切 ...
- centos终端显示-bash-4.2#解决方法
登录linux系统过后,发现显示的是-bash-4.2# 而不是root@主机名 + 路径的显示方式,发生这种情况的原因是根目录下缺失几个配置文件,从默认配置中拷贝过来就可以解决了: cp /etc/ ...
- 英文版windows乱码问题(win7/8/10)