WPF 与 StringFormat 绑定在工具提示上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/197095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF binding with StringFormat doesn't work on ToolTips
提问by huseyint
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:
下面的代码有一个简单的绑定,它使用完全相同的绑定符号将名为 MyTextBlock 的 TextBlock 的 Text 绑定到 TextBox 的 Text 和 ToolTip 属性:
<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>
The binding also uses the StringFormat property introduced with .NET 3.5 SP1which 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?
该绑定还使用.NET 3.5 SP1 引入的StringFormat 属性,该属性对于上述 Text 属性工作正常,但对于 ToolTip 似乎已损坏。预期的结果是“It is: Foo Bar”,但是当您将鼠标悬停在 TextBox 上时,工具提示仅显示绑定值,而不是字符串格式的值。有任何想法吗?
采纳答案by huseyint
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.
我更喜欢更简单的语法,就像我原来的问题中的那样。
回答by Matt Hamilton
ToolTips in WPF can contain anything, not just text, so they provide a ContentStringFormat property for the times you just want text. You'll need to use the expanded syntax as far as I know:
WPF 中的工具提示可以包含任何内容,而不仅仅是文本,因此它们在您只需要文本时提供 ContentStringFormat 属性。据我所知,您需要使用扩展语法:
<TextBox ...>
<TextBox.ToolTip>
<ToolTip
Content="{Binding ElementName=myTextBlock,Path=Text}"
ContentStringFormat="{}It is: {0}"
/>
</TextBox.ToolTip>
</TextBox>
I'm not 100% sure about the validity of binding using the ElementName syntax from a nested property like that, but the ContentStringFormat property is what you're looking for.
我不是 100% 确定使用 ElementName 语法从嵌套属性绑定的有效性,但 ContentStringFormat 属性正是您要寻找的。
回答by MuiBienCarlota
It's could be a bug. When you use short syntax for tooltip:
这可能是一个错误。当您对工具提示使用简短语法时:
<TextBox ToolTip="{Binding WhatEverYouWant StringFormat='It is: \{0\}'}" />
StringFormat is ignore but when you use expanded syntax:
StringFormat 被忽略,但当您使用扩展语法时:
<TextBox Text="text">
<TextBox.ToolTip>
<TextBlock Text="{Binding WhatEverYouWant StringFormat='It is: \{0\}'}"/>
</TextBox.ToolTip>
</TextBox>
It works as expected.
它按预期工作。
回答by Lucas Locatelli
As Matt said ToolTip can contain anything inside so for your you could bind a TextBox.Text inside your ToolTip.
正如马特所说,工具提示可以包含任何内容,因此您可以在工具提示中绑定一个 TextBox.Text。
<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>
Even you can Stack a grid inside the ToolTip and layout your text if you want.
即使您可以在工具提示内堆叠网格并根据需要布置文本。
回答by Athari
Your code can be as short as this:
您的代码可以这么短:
<TextBlock ToolTip="{Binding PrideLands.YearsTillSimbaReturns,
Converter={StaticResource convStringFormat},
ConverterParameter='Rejoice! Just {0} years left!'}" Text="Hakuna Matata"/>
We'll use the fact Converters are never ignored, unlike StringFormat.
与 StringFormat 不同,我们将使用转换器永远不会被忽略的事实。
Put this into StringFormatConverter.cs:
将其放入StringFormatConverter.cs:
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();
}
}
}
Put this into your ResourceDictionary.xaml:
把它放到你的ResourceDictionary.xaml 中:
<conv:StringFormatConverter x:Key="convStringFormat"/>
回答by Сергей Игнахин
In this situation, you can use relative binding:
在这种情况下,您可以使用相对绑定:
<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>