Why does MultiBinding with a Converter not work within a ToolTip?

删除回忆录丶 提交于 2020-01-13 04:56:26

问题


For part of a fairly-complex WPF ToolTip, I'm attempting to use a MultiBinding to produce formatted text based on two properties. The problem is, the binding's MultiConverter receives DependencyProperty.UnsetValue for each item in its values array.

The following works, using a single Binding:

<ToolTipService.ToolTip>
  <StackPanel>
    <TextBlock>
      <TextBlock.Text>
        <Binding Path="Amt" Converter="{StaticResource singleValueConverter}"/>
      </TextBlock.Text>
    </TextBlock>        
  </StackPanel>
</ToolTipService.ToolTip>

And so does this, using a MultiBinding with 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>

But a MultiBinding with a Converter does not:

<ToolTipService.ToolTip>
  <StackPanel>
    <TextBlock>
      <TextBlock.Text>
        <MultiBinding Converter="{StaticResource multiValueConverter}">
          <Binding Path="Amt"/>
          <Binding Path="Currency"/>
        </MultiBinding>
      </TextBlock.Text>
    </TextBlock>        
  </StackPanel>
</ToolTipService.ToolTip>

The bindings in the last example don't receive any value. This isn't the case outside of a ToolTip - what is going on such that binding fails in this specific case?


回答1:


Try setting Mode="OneWay" on your binding.

Also, have you checked this problem and solution: 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 "";
    [...]
}



回答2:


Try this:

<ToolTipService.ToolTip>
    <StackPanel>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource multiValueConverter}">
                    <MultiBinding.Bindings>
                        <BindingCollection>
                            <Binding Path="Amt"/>
                            <Binding Path="Currency"/>
                        </BindingCollection>
                    </MultiBinding.Bindings>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>        
    </StackPanel>
</ToolTipService.ToolTip>


来源:https://stackoverflow.com/questions/6273083/why-does-multibinding-with-a-converter-not-work-within-a-tooltip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!