我想在工具提示中绑定文本,但我有一个问题,它的绑定值是其他元素控件,因此我基本上无法通过绑定获取它们的值。
<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>
基本上我尝试绑定这段代码。
如果您查看输出,您将看到一个错误:
System.Windows.Data 错误:4:找不到绑定源
参考“ElementName=txb2”。绑定表达式:路径=文本;
数据项=空;目标元素是“Run”(HashCode=58577354);目标
属性是“文本”(类型“字符串”)
您可以使用 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:Reference的区别看看下面thread https://stackoverflow.com/questions/19244111/what-is-the-difference-between-xreference-and-elementname。 ElementName 不起作用,因为 Tooltip 不是 Ui 属性,但 ElementName 仅在搜索 txb2 时与 Ui 元素层次结构(可视化树)一起使用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)