1

I'm having a problem with a MultiValueConverter that feels more and more like a .NET bug the more I try to fix it. Here's what's up:

I am binding the IsEnabled property of both a Stackpanel and a User Control (UC) using a similar MultiBinding definition (of course, I'm using <ns:usercontrolname.IsEnabled> for the UC):

<StackPanel.IsEnabled>
    <MultiBinding Converter="{StaticResource InfoSectionIsEnabled}">
        <Binding Path="IsInactive" />
        <Binding Path="IsWaitingForMicro" />
    </MultiBinding>
</StackPanel.IsEnabled>

The 'current' UC (the one containing this XAML) is being assigned to a TabItem.Content. The DataContext is set at the TabItem.

I'm also using the Properties above in two separate, single-value bindings on the same 'current' UC:

<scps:ucSOIA x:Name="ucSOIA" IsEnabled="{Binding Path=IsWaitingForMicro, Converter={StaticResource InvertBool}}"/>

and

<scps:ucMisc x:Name="ucMisc" IsEnabled="{Binding Path=IsInactive, Converter={StaticResource InvertBool}}"/>

All the bindings work fine when the 'current' UC is first instantiated and assigned to the TabItem's Content. But as soon as I change the TabItem's Content to a different UC (without disposing of the instantiated, 'current' UC), all converters are called again and the MultiValueConverter's values() parameter contains NamedObject for both entires. The single-value converters, using the exact same Properties, work fine.

I can't figure this out. I've even tried using RelativeSource on the MultiBindings, to no avail.

I've got two questions:

  1. How can the values in the MultiBindingConverter be NamedObject when the single-value bindings work fine

  2. The less-important question is why are the bindings being called when I change the TabItem's Content (note, these converters are being called prior to UserControl.Unloaded)

Thanks, Dave

D. Dubya
  • 189
  • 1
  • 12
  • I'm getting a similar issue. My multibinding is throwing an exception at design time (Conversion from type 'NamedObject' to type 'Boolean' is not valid.). If I comment the multibinding, but continue using those bindings (seperately) on two other controls, it all works. It seems to be something to do with the multibinding? – Smudge202 Jun 06 '11 at 12:32

1 Answers1

3

Worked it out, to an extent at least. You need to be more robust with your multibinding converter (InfoSectionIsEnabled).

In my case, my converter was as follows:

Public Function Convert(values() As Object,
                        targetType As System.Type,
                        parameter As Object,
                        culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
    If values IsNot Nothing Then
        Return values.All(Function(n) CBool(n))
    End If
    Return False
End Function

I then spotted in another post (lost the link already, sorry) that at design time, because the IDE cannot evaluate the correct values to pass to the converter, it passes a 'NamedObject' for each of the bindings.

In my case, I was only concerned with Booleans, so I changed my code to specifically deal with those cases only (thereby preventing accidentally attempting to cast the NamedObjects to boolean):

Public Function Convert(values() As Object,
                        targetType As System.Type,
                        parameter As Object,
                        culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
    If values IsNot Nothing AndAlso values.All(Function(n) TypeOf n Is Boolean) Then
        Return values.All(Function(n) CBool(n))
    End If
    Return False
End Function

This immediately resolved the issue for me.

Smudge202
  • 4,689
  • 2
  • 26
  • 44
  • Boy do I feel embarrassed... lol. I didn't even think to add the validation to my converter. Thanks for your help! But it still doesn't account for why the MultiBinding fails in this way while the single-value converters work perfectly fine. – D. Dubya Jun 06 '11 at 13:38
  • At least you wasn't the only one ;-) – Smudge202 Jun 06 '11 at 13:39