Andy Clarke
Andy Clarke

Reputation: 3262

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?

Upvotes: 0

Views: 700

Answers (2)

Andy Clarke
Andy Clarke

Reputation: 3262

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!

Upvotes: 0

CodeNaked
CodeNaked

Reputation: 41403

Before the DataContext is pass down, you will get DependencyProperty.UnsetValue and need to handle that case. So you can do something like:

bool isManualChange= (value[1] is bool) ? (bool)value[1] : false; // or true depending on which should be the default

From the link above:

UnsetValue is a sentinel value that is used for scenarios where the WPF property system is unable to determine a requested DependencyProperty value. UnsetValue is used rather than null, because null could be a valid property value, as well as a valid (and frequently used) DefaultValue.

So basically the DataContext is currently null, so it can't find a property named IsManualChange so you get UnsetValue.

Once the context is setup, your converter should get called again.

Upvotes: 1

Related Questions