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