WPF 与 StringFormat 绑定在工具提示上不起作用 [英] WPF binding with StringFormat doesn't work on ToolTips

查看:25
本文介绍了WPF 与 StringFormat 绑定在工具提示上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码有一个简单的绑定,它使用完全相同的绑定符号将名为 MyTextBlock 的 TextBlock 的 Text 绑定到 TextBox 的 Text 和 ToolTip 属性:

The following code has a simple binding which binds the Text of the TextBlock named MyTextBlock to TextBox's Text and ToolTip property using the exact same Binding notation:

<StackPanel>
    <TextBlock x:Name="MyTextBlock">Foo Bar</TextBlock>
    <TextBox    Text="{Binding ElementName=MyTextBlock, Path=Text, StringFormat='It is: {0}'}"
             ToolTip="{Binding ElementName=MyTextBlock, Path=Text, StringFormat='It is: {0}'}" />
</StackPanel>

绑定还使用 StringFormat 属性随 .NET 3.5 SP1 一起引入,对于上述 Text 属性工作正常,但对于 ToolTip 似乎已损坏.预期的结果是It is: Foo Bar",但是当您将鼠标悬停在 TextBox 上时,工具提示仅显示绑定值,而不是字符串格式的值.有什么想法吗?

The binding also uses the StringFormat property introduced with .NET 3.5 SP1 which is working fine for the above Text property but seems to be broken for the ToolTip. The expected result is "It is: Foo Bar" but when you hover over the TextBox, the ToolTip shows only the binding value, not the string formatted value. Any ideas?

推荐答案

以下是一个冗长的解决方案,但它有效.

The following is a wordy solution but it works.

<StackPanel>
  <TextBox Text="{Binding Path=., StringFormat='The answer is: {0}'}">
    <TextBox.DataContext>
      <sys:Int32>42</sys:Int32>
    </TextBox.DataContext>
    <TextBox.ToolTip>
      <ToolTip Content="{Binding}" ContentStringFormat="{}The answer is: {0}" />
    </TextBox.ToolTip>
  </TextBox>
</StackPanel>

我更喜欢更简单的语法,就像我最初的问题中的那样.

I would prefer a much simpler syntax, something like the one in my original question.

这篇关于WPF 与 StringFormat 绑定在工具提示上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆