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

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

问题描述

以下code有一个简单的绑定结合命名使用完全相同的绑定符号MyTextBlock到文本框的文本和工具提示财产TextBlock的文字:

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>

的结合也使用<一href=\"http://blogs.msdn.com/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx\">StringFormat财产.NET 3.5 SP1 这是工作的罚款上面的Text属性,但似乎为工具提示被打破出台。预期的结果是它是:富酒吧但是当你将鼠标悬停在文本框,工具提示只显示绑定值,而不是字符串格式化的值。任何想法?

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>

我想preFER一个简单得多的语法,像一个在我原来的问题。

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

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

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