[英]How to binding other element in ToolTip
I want binding Text in Tooltip but i have one problem, it is binding value is other element controls, therefore i cannot basically get their value through binding. 我想在工具提示中绑定文本,但是我有一个问题,它的绑定值是其他元素控件,因此我基本上无法通过绑定获取它们的值。
<TextBlock x:Name="txb2" Text="Hello Stackoverflow"/>
<TextBox Grid.Row="1" TextChanged="TextBox_TextChanged">
<TextBox.ToolTip>
<TextBlock>
<Run Text="{Binding ElementName=txb2, Path=Text}" FontWeight="Bold"/>
</TextBlock>
</TextBox.ToolTip>
</TextBox>
basically I tried binding this code. 基本上我尝试绑定此代码。
If you look at the output you will see an error: 如果查看输出,将看到错误:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=txb2'.
System.Windows.Data错误:4:找不到参考'ElementName = txb2'的绑定源。 BindingExpression:Path=Text;
BindingExpression:Path = Text; DataItem=null;
DataItem = null; target element is 'Run' (HashCode=58577354);
目标元素是“运行”(HashCode = 58577354); target property is 'Text' (type 'String')
目标属性为“文本”(类型为“字符串”)
You can fix it by using x:Reference: 您可以使用x:Reference修复它:
<TextBlock x:Name="txb2" Text="Hello Stackoverflow"/>
<TextBox Grid.Row="1">
<TextBox.ToolTip>
<TextBlock>
<Run Text="{Binding Source={x:Reference txb2}, Path=Text}" FontWeight="Bold"/>
</TextBlock>
</TextBox.ToolTip>
</TextBox>
As for the difference between ElementName and x:Reference take a look at the following thread . 至于ElementName和x:Reference之间的区别,请看以下线程 。 ElementName does not work since Tooltip is not a Ui property, but ElementName only works with Ui Element hierarchy (Visual Tree) when it searches txb2.
由于Tooltip不是Ui属性,因此ElementName不起作用,但是ElementName在搜索txb2时仅适用于Ui元素层次结构(可视树)。
Tooltips exist outside the visual tree, so can't reference other controls by name. 工具提示存在于视觉树之外,因此无法按名称引用其他控件。 All that a tooltip knows about is its own PlacementTarget – the UIElement that it is displayed against.
工具提示仅知道其自己的PlacementTarget-针对其显示的UIElement。
One way to allow the tooltip to reference other controls is to hijack some otherwise unused property of this placement target control (Tag is most often suitable), which can then be referenced by the tooltip. 允许工具提示引用其他控件的一种方法是劫持此放置目标控件的一些其他未使用的属性(Tag最适合),然后可以由工具提示引用。
<TextBox x:Name="txb2" Text="Hello Stackoverflow" Width="200" />
<TextBox Grid.Row="1" Tag="{Binding ElementName=txb2}" Width="200">
<TextBox.ToolTip>
<ToolTip DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<TextBlock>
<Run Text="{Binding Text}" FontWeight="Bold" />
</TextBlock>
</ToolTip>
</TextBox.ToolTip>
</TextBox>
if you're using the MVVM design pattern, an alternative method (that doesn't require property hijacking) is to bind to the PlacementTarget's DataContext (usually the ViewModel). 如果您使用的是MVVM设计模式,则另一种方法(不需要属性劫持)是绑定到PlacementTarget的DataContext(通常是ViewModel)。 You can then bind the tooltip's content to whatever property of that you like.
然后,您可以将工具提示的内容绑定到所需的任何属性。
<ToolTip DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.