原文地址:https://wpf.2000things.com/2010/10/26/106-set-property-value-to-point-to-another-object/
有时候在XAML中需要设置当前元素某个属性指向另外一个元素。
比如CommandTarget 属性,它被用来判断当前控件是否是command的发起者。
举个例子,你有一个用来执行拷贝命令的Button ,你希望内容可以拷贝到一个TextBox中,那么你就得设置Button的CommandTarget 属性指向这个TextBox。
在XAML中通过Binding 拓展标签来实现,通过设置绑定的ElementName 属性去指向期望的控件。
<Button Content="Paste" Command="ApplicationCommands.Paste"
CommandTarget="{Binding ElementName=myTextBox}" />
<TextBox Name="myTextBox"/>
XAML2009允许使用x:Reference 拓展标签来实现这个,但是在WPF的XAML中不支持。
<Button Content="Paste" Command="ApplicationCommands.Paste"
CommandTarget="{x:Reference myTextBox}" />
<TextBox Name="myTextBox"/>