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的早期,对依赖属性理解一直都非常的不到位,其恶果就是,我每次在写依赖属性的时候,需要翻过去的代码来复制黏贴. 相信很多朋友有着和我相同的经历,所以这篇文章希望能帮助到那些刚刚开始学 ...
随机推荐
- HDU-1090-A+B for Input-Output Practice (II)(骗訪问量的)
A+B for Input-Output Practice (II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- mongodb进阶三之mongodb管理
http://blog.csdn.net/stronglyh/article/details/46827141 平时的开发环境win比較多啊,但生产环境要放到unix环境上 一:命令 安装就不少了,网 ...
- sql 中 ALTER 和 UPDATE 的区别
alter 是DDL语句,是修改数据库中对象(表,数据库,视图..)的语句. 如需在表中添加列,请使用下面的语法: ALTER TABLE table_name ADD column_name dat ...
- Fiddler-抓取安卓手机APP请求地址
第一步:下载神器Fiddler,下载链接: http://fiddler2.com/get-fiddler 下载完成之后,傻瓜式的安装一下了! 第二步:设置Fiddler打开Fiddler, ...
- 《转》oracle—flashback
FLASHBACK介绍 在介绍flashback之前先介绍下undo_retention相关参数 undo_retention:表示undo数据的过期时间.系统默认这个时间设置为900即15分钟.但要 ...
- 使用css3属性transition实现页面滚动
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...
- docker容器互连
三种方式 1.使用容器连接的示例如下: $ docker run --name some-app --link itbilu-mysql:mysql -d application-that-uses- ...
- php.ini配置与中国间隔12小时间设置方法
打开php.ini 配置文件找到date.timezone把=号后面的参数改成这个date.timezone = Etc/GMT+4即可,这样与中国的时间误差即能达到12小时
- hdu4711Weather 概率dp
//第i个城市到第j个城市的概率ma[i][j] //第i天的天气天气wet[i] //第i个城市天气为j的概率wet_m[i][j] //Hovey从0点開始,找出其概率最大的路线 //dp[i][ ...
- mysql之slave_skip_errors选项
要说slave_skip_errors选项,就不得不提mysql的replication机制,总的来说它分了三步来实现mysql主从库的同步 master将改变记录到二进制日志(binary log) ...