使用StringFormat的WPF绑定不';不能在工具提示上工作

使用StringFormat的WPF绑定不';不能在工具提示上工作,wpf,binding,Wpf,Binding,下面的代码有一个简单的绑定,它使用完全相同的绑定符号将名为MyTextBlock的TextBlock的文本绑定到TextBox的文本和工具提示属性: <StackPanel> <TextBlock x:Name="MyTextBlock">Foo Bar</TextBlock> <TextBox Text="{Binding ElementName=MyTextBlock, Path=Text, StringFormat='It i

下面的代码有一个简单的绑定,它使用完全相同的绑定符号将名为MyTextBlock的TextBlock的文本绑定到TextBox的文本和工具提示属性:

<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>

富吧

绑定还使用了,对于上面的文本属性,它工作得很好,但是对于工具提示,它似乎被破坏了。预期结果是“It is:Foo Bar”,但当您将鼠标悬停在文本框上时,工具提示仅显示绑定值,而不是字符串格式的值。有什么想法吗?

WPF中的工具提示可以包含任何内容,而不仅仅是文本,因此它们为您只需要文本的时间提供ContentStringFormat属性。据我所知,您需要使用扩展语法:

<TextBox ...>
  <TextBox.ToolTip>
    <ToolTip 
      Content="{Binding ElementName=myTextBlock,Path=Text}"
      ContentStringFormat="{}It is: {0}"
      />
  </TextBox.ToolTip>
</TextBox>


我不能100%确定使用像这样的嵌套属性中的ElementName语法进行绑定的有效性,但ContentStringFormat属性正是您所需要的。

下面是一个冗长的解决方案,但它可以工作

<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>

42

我希望使用更简单的语法,类似于我原来问题中的语法。

您的代码可以如此简短:

<TextBlock ToolTip="{Binding PrideLands.YearsTillSimbaReturns,
    Converter={StaticResource convStringFormat},
    ConverterParameter='Rejoice! Just {0} years left!'}" Text="Hakuna Matata"/>
将其放入您的资源字典。xaml

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace TLKiaWOL
{
    [ValueConversion (typeof(object), typeof(string))]
    public class StringFormatConverter : IValueConverter
    {
        public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (ReferenceEquals(value, DependencyProperty.UnsetValue))
                return DependencyProperty.UnsetValue;
            return string.Format(culture, (string)parameter, value);
        }

        public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
<conv:StringFormatConverter x:Key="convStringFormat"/>

正如马特所说,工具提示可以包含任何内容,因此您可以在工具提示中绑定文本框

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

富吧
甚至你也可以在工具提示中堆叠一个网格,如果你想的话,也可以布局你的文本。

这可能是一个bug。 在工具提示中使用简短语法时:

<TextBox ToolTip="{Binding WhatEverYouWant StringFormat='It is: \{0\}'}" />

StringFormat为“忽略”,但使用扩展语法时:

<TextBox Text="text">
   <TextBox.ToolTip>
      <TextBlock Text="{Binding WhatEverYouWant StringFormat='It is: \{0\}'}"/>
   </TextBox.ToolTip>
</TextBox>


它按预期工作。

在这种情况下,您可以使用相对绑定:

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

富吧

我明白了,我认为工具提示只是一个普通的字符串,就像在Windows窗体中一样。是的,本例中的ElementName语法无法访问外部元素。请注意,只有将{0}放在字符串开头时,{0}才是必需的,因此需要它来区别于其他xaml标记。Mind=blow。我刚刚点击了这个,就像“waaat?”它真的让我很恼火,因为stringformat在没有指定转换器的情况下不能“正常工作”。我必须写我自己的stringformatConverter。MS再次落球…
StringFormat
仅在
TargetType
为字符串类型时应用<代码>工具提示内容的类型为
对象
@Shimmy:“更好”在旁观者的眼中,可以将您自己的问题标记为已接受answer@Shimmy更糟糕的是,他的回答中还包括一个“42”的笑话。@Andomar,人们用投票来决定更好,尤其是在这里,答案几乎是一样的。让ppl回答你的问题,然后复制他们的答案并赢得声誉,这是一种完全错误的态度。我无法让下面建议的解决方案中的任何一个起作用,但这一个做到了:最准确的答案。。谢谢无论你想编译什么,后面都少了一个逗号虽然我更喜欢上面的答案,但ElementBinding问题把我绊倒了。这个答案适用于我的情况,而其他人则不适用。