问题描述
限时送ChatGPT账号..我想在 Tooltip 中绑定 Text 但我有一个问题,它绑定的值是其他元素控件,因此我基本上无法通过绑定获取它们的值.
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 错误:4:无法找到用于绑定的源参考 'ElementName=txb2'.BindingExpression:Path=Text;数据项=空;目标元素是运行"(HashCode=58577354);目标属性是文本"(类型字符串")
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=txb2'. BindingExpression:Path=Text; DataItem=null; target element is 'Run' (HashCode=58577354); target property is 'Text' (type 'String')
您可以使用 x:Reference 修复它:
You can fix it by using 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>
至于ElementName和x的区别:参考看下面线程.ElementName 不起作用,因为 Tooltip 不是 Ui 属性,但 ElementName 在搜索 txb2 时仅适用于 Ui 元素层次结构(可视化树).
As for the difference between ElementName and x:Reference take a look at the following thread. 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 中绑定其他元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论