Multibinding不会重新评估集合的第一个元素。

huangapple go评论53阅读模式
英文:

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

问题

The issue you're encountering with the Multibinding not accessing a property on the first element of the collection may be related to the order of evaluation in your XAML. Without a specific error message or more context, it's challenging to pinpoint the exact cause.

这个问题可能与XAML中的评估顺序有关。没有具体的错误消息或更多上下文,很难准确确定问题的根本原因。

If you want to troubleshoot this further, you might consider checking the following:

如果您想进一步排除问题,可以考虑检查以下内容:

  1. Ensure that the DataContext is correctly set for the first element in your collection. It should have the Section property accessible.

确保第一个元素的DataContext正确设置,应该能够访问Section属性。

  1. Check if there are any binding errors in your XAML output window when running your application. Binding errors can sometimes provide clues about what's going wrong.

在运行应用程序时,检查XAML输出窗口中是否有任何绑定错误。绑定错误有时可以提供关于出现问题的线索。

  1. Consider simplifying your XAML code for testing purposes. Remove any unnecessary complexity to isolate the issue.

出于测试目的,考虑简化您的XAML代码。去除任何不必要的复杂性以隔离问题。

  1. Ensure that there are no conflicting bindings or triggers in your XAML that might interfere with the evaluation of the Multibinding.

确保XAML中没有冲突的绑定或触发器,可能会干扰Multibinding的评估。

If you've already checked these aspects and the issue persists, it might require more in-depth debugging and analysis of your XAML and code.

英文:

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?

答案1

得分: 0

不幸的是,问题位于提取的代码上方一点:

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

同一组中的先前RadioButton使用了相同的索引值"2"来选中RadioButton,因此当我再次单击我的选中的RadioButton时,先前的那个被选中(但不可见),因此取消了我的RadioButton的选中状态。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月13日 00:11:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定