1

Problem with MultiBinding Converter
I know it does not make sense to send the same value twice
I do that to isolate on that single binding
In the converter I get an error on cast to bool
The value is

{DependencyProperty.UnsetValue}

But "RWnet" is a bool

<ContentPresenter Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
        x:Name="fieldTemplateDetail"
        Content="{Binding}"
        ContentTemplateSelector="{StaticResource fieldTemplateSelector}">
    <ContentPresenter.Visibility>
        <MultiBinding Converter="{StaticResource visabilityConverterTwoBool}">
            <Binding Path="RWnet" />
            <Binding Path="RWnet" />
        </MultiBinding>
    </ContentPresenter.Visibility>
</ContentPresenter>

public class VisabilityConverterTwoBool : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values == null) return Visibility.Visible;
        if (values.Length != 2) return Visibility.Visible;
        try
        {
            if ((bool)values[0] == false) return Visibility.Collapsed;
            if ((bool)values[1] == false) return Visibility.Collapsed;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message, "VisabilityConverterTwoBool");
            Debug.WriteLine(values[0].ToString());
            Debug.WriteLine(values[1].ToString());
        }
        return Visibility.Visible;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }
}

If I send the other half of the actual Multi Binding it works (wrong result but no error)

<Binding ElementName="cbEditMode" Path="IsChecked" />

But this works just fine
RWnet is a bool

<ContentPresenter Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
        Visibility="{Binding Path=RWnet, Converter={StaticResource bvc}}"
        x:Name="fieldTemplateDetail"
        Content="{Binding}"
        ContentTemplateSelector="{StaticResource fieldTemplateSelector}"/>

<BooleanToVisibilityConverter x:Key="bvc" />

for McGarnagle this is the data context

<ListBox  Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" x:Name="lbCurDocFields" 
            ItemsSource="{Binding Source={StaticResource curDocFields}}" LostFocus="lbCurDocFields_LostFocus"
paparazzo
  • 44,497
  • 23
  • 105
  • 176

1 Answers1

1

Depending on how you're wiring up the data context, there is no guarantee a binding will be ready when you expect it to be. Validate the inputs fully, it should fix the issue:

if (values.Length != 2 && values[0] as bool? == null || values[1] as bool? == null)
    return Visibility.Visible;
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • But it has a data context available. See the example of what works at the bottom. That ContentPresenter displays other properties from the same binding. I will post the ItemsSource binding. – paparazzo Feb 06 '14 at 23:47
  • @Blam it displays properties, but are you sure it didn't display an "unset value", first, for a split-second, before the DataContext was applied? – McGarnagle Feb 06 '14 at 23:52
  • Even though I did not understand your answer I plugged it in and it seemed to work. +1 for now. If I cannot break it I will give it a check. – paparazzo Feb 07 '14 at 00:21
  • @Blam it's just ... binding doesn't necessarily occur in order. The `ContentPresenter` doesn't break, because it can handle an UnsetValue. Likewise, it's generally best to assume that any inputs that arrive via a binding could be unset / null / invalid. – McGarnagle Feb 07 '14 at 00:27
  • But how is it different with the direct single binding that works (and also goes to a converter)? Does the bool? on the multi force it to wait for a real value? And I so get frustrated with people that won't try my answer because they don't understand it. – paparazzo Feb 07 '14 at 00:34
  • @Blam well ... assuming `BooleanToVisibilityConverter` also does not validate the bool, and still works ... I don't really know. I will speculate that maybe MultiBinding works differently, in that when the first binding arrives, it runs the conversion function with one value set and the other unset ; then when the second binding arrives, it runs again. – McGarnagle Feb 07 '14 at 00:42
  • Even if I substitute in my my own single value converter it receives bool (not {DependencyProperty.UnsetValue}). But you answer seems to fix it multi - I was hoping you had some magic. – paparazzo Feb 07 '14 at 01:23