Search code examples
wpfxamldata-bindingconvertersmultibinding

Multibinding doesn't re-evaluate the first element of a collection


Is there a reason for a first element of a collection to not allow multibinding refresh?

My Multibinding linked to a converter works fine for 4 elements of a collection: the click Check the radio button, the second click does nothing (because the evaluation is still right)

<DataTemplate DataType="{x:Type vm:RoomSettingsViewModel}">
<RadioButton GroupName="currentSectionGroup" Style="{StaticResource SettingsTabRadioButtonStyle}" Content="{Binding Room.Name}" ToolTipService.InitialShowDelay="2000" ToolTipService.BetweenShowDelay="0">
<RadioButton.IsChecked>
    <MultiBinding Converter="{StaticResource EqualityToBoolMultiConv}" >
    <Binding Path="DataContext.CurrentSectionIndex" ElementName="userControl" />
    <Binding Path="Section" Mode="OneWay"/>
    </MultiBinding>
    </RadioButton.IsChecked>
</RadioButton>
</DataTemplate>

But, only for the first element of the collection, the click on the already checked radio button doesn't GET the Section property (verified with a breakpoint)

This one seems to be not evaluated at all and the equality evaluation fails: the radio button become unchecked.

The Section:

public int Section { get; private set; }

The datacontext.CurrentSectionIndex:

public int CurrentSectionIndex
{
    get
    {
        return _currentSectionIndex;
    }
    set
    {
        //if (_currentSectionIndex == value)
        //return;

        if (!CanQuitCurrentSection())
            return;

        if (_vmDic.TryGetValue(value, out AudioVideoSettingsSectionViewModelBase vm))
        {
            _currentSectionIndex = value;
            CurrentViewModel = vm;
        }
    }
}

About the commented code: this help to resolve the issue because the equality isn't reevaluated at second click. Nevertheless, it doesn't explain the buggy behavior.

The converter:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    if (values.Length < 2)
        return false;

    if (!int.TryParse(values[0].ToString(), out int currentSection))
        return false;

    if (!int.TryParse(values[1].ToString(), out _section))
        return false;

    _section = (int)values[1];

    return currentSection == _section;
}

At second click on a RadioButton (ex:B), each radio button (A, C and D) are re-evaluated THEN the clicked button is re-evaluated. But in the first radiobutton case, after all other radiobuttons re-evaluations, the first one evaluation isn't done. And so the first radiobutton is "uncheck" (default style).

Is there anybody who understand why a multibinding would not access to a property item only on the first element of the collection?


Solution

  • Infortunally, the issue was located a little above the code extracted :

    <RadioButton GroupName="currentSectionGroup" Visibility="{Binding VideoHuBVisibility}" IsChecked="{Binding CurrentSectionIndex, Converter={StaticResource StringEqualityToBoolConv}, ConverterParameter=2}">
       <StackPanel Orientation="Horizontal">
        <TextBlock Text="VIDEOHUB" VerticalAlignment="Center"/>
       </StackPanel>
    </RadioButton>
    

    A previous RadioButton of the same group used the same index value "2" to check the radiobutton so when I click again on my checked radio button, the previous one was checked (but not visible) and so it unchecked my radio button.