Search code examples
wpfmvvmmultibinding

MultiBindingConvertor not working on grid in WPF MVVM application


I've got a column in a WPF Telerik grid I need to restrict based on two things.

An IsEditable property on ViewModel and an IsManualChange property which is Property of the List the grid is bound on ...

I wrote a MultiBoolConvertor to handle this and implemented in the WPF as follows:

<telerik:GridViewComboBoxColumn 
    Header="Selection" 
    DataMemberBinding="{Binding HandHeldDifference.GRSSelection}" 
    ItemsSource="{Binding Path=SelectionOptions}">
    <telerik:GridViewComboBoxColumn.IsReadOnly>
        <MultiBinding Converter="{StaticResource MultiBoolConv}" 
            ConverterParameter="True">
            <Binding 
                RelativeSource="{RelativeSource FindAncestor, 
                    AncestorType={x:Type StackPanel}}" 
                Path="DataContext.IsEditable" />
            <Binding Path="IsManualChange" />
        </MultiBinding>
    </telerik:GridViewComboBoxColumn.IsReadOnly>
</telerik:GridViewComboBoxColumn>

However the values that come into the Convertor are a bool (from the ViewModel) and a DependencyProperty.UnsetValue from the IsManualChange!

public object Convert(object[] values, 
                        Type targetType, 
                        object parameter, 
                        CultureInfo culture)
{
    var defaultReturn = false;
    if (parameter != null)
    {
        bool.TryParse(parameter.ToString(), out defaultReturn);
    }

    if (values == null) return defaultReturn;
    if (values.Length < 2) return defaultReturn;
    if (values[0] is bool && values[1] is bool)
    {
        return ((bool) values[0]) && ((bool) values[1]);
    }
    return defaultReturn;
}

The second value obviously fails the "values[1] is bool" comparison

A clue maybe that the converter is only being called once, rather than per line as I'd expect.

Does anyone know how I can get this working please?


Solution

  • Turns out the Telerik grid binds the IsReadOnly to the ViewModel and you have to use IsReadOnlyBinding when you want to bind to an item in the ItemsSource!