wpf - 为什么带有转换器的多重绑定(bind)在工具提示中不起作用?

标签 wpf data-binding multibinding

对于相当复杂的 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>

使用 MultiBindingStringFormat 也是如此:

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

但是带有 ConverterMultiBinding 则不会:

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

相关文章:

.Net BindingDictionary

c# - 转换器中的 WPF MultiBinding 失败 ==> DependencyProperty.UnsetValue

wpf - 在 Label.ContentTemplate 中绑定(bind)

c++ - 将消息注入(inject) WPF 应用程序消息泵

c# - 迁移后的 Reactive 6.5.0 ReactiveCommand

c# - 将 SelectedItems 绑定(bind)到 ObservableCollection 属性

c# - 设置对话框窗口的所有者时出现异常

wpf - 单元测试 WPF 绑定(bind)

c# - 将文本转换为多重绑定(bind)

c# - WPF 多重绑定(bind)失败。为什么?