对于相当复杂的 WPF 工具提示的一部分,我尝试使用 MultiBinding 来生成基于两个属性的格式化文本。问题是,绑定(bind)的 MultiConverter 接收其 values
数组中每个项目的 DependencyProperty.UnsetValue
。
使用单个绑定(bind)
可以实现以下效果:
<ToolTipService.ToolTip>
<StackPanel>
<TextBlock>
<TextBlock.Text>
<Binding Path="Amt" Converter="{StaticResource singleValueConverter}"/>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</ToolTipService.ToolTip>
使用 MultiBinding
和 StringFormat
也是如此:
<ToolTipService.ToolTip>
<StackPanel>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat='{0:C} in {1}'>
<Binding Path="Amt"/>
<Binding Path="Currency"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</ToolTipService.ToolTip>
但是带有 Converter
的 MultiBinding
则不会:
<ToolTipService.ToolTip>
<StackPanel>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource multiValueConverter}">
<Binding Path="Amt"/>
<Binding Path="Currency"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</ToolTipService.ToolTip>
上一个示例中的绑定(bind)不接收任何值。工具提示之外的情况并非如此 - 在这种特定情况下发生了什么导致绑定(bind)失败?
最佳答案
尝试在绑定(bind)上设置 Mode="OneWay"。
另外,您检查过这个问题和解决方案吗: http://social.msdn.microsoft.com/Forums/en-IE/wpf/thread/15ada9c7-f781-42c5-be43-d07eb1f90ed4
The reason of this error is the tooltips have not been loaded, so DependencyProperty.GetValue returns DependencyProperty.UnsetValue. You should add some code to test that is value is Dependency.UnsetValue. The following code shows how to do this.
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue)
return "";
[...]
}
关于wpf - 为什么带有转换器的多重绑定(bind)在工具提示中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6273083/