使用MultiBinding切换绑定源

opi*_*ahn 1 data-binding wpf observablecollection

我有一个带有两个ObservableCollections的MultiBinding的DataBinding,我想在具有MultiConverter的条件下在它们之间切换.因此转换器提供了正确的集合,但绑定似乎没有更新.

有任何想法吗??

映入眼帘,

于尔根

Ela*_*atz 5

这是您需要的转换器:

public class SwitchCollectionsConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool b = (bool)values[2];

        if (b)
            return values[0];
        else
            return values[1];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

注册转换器:

    <local:SwitchCollectionsConverter x:Key="TheConverter" />
Run Code Online (Sandbox Code Playgroud)

绑定的用法:

    <ItemsControl>
        <ItemsControl.ItemsSource>
            <MultiBinding Converter="{StaticResource TheConverter}">
                <Binding Path="FirstCollection" />
                <Binding Path="SecondCollection" />
                <Binding Path="IsFirst" />
            </MultiBinding>
        </ItemsControl.ItemsSource>
    </ItemsControl>
Run Code Online (Sandbox Code Playgroud)

假设您在DataContext中有FirstCollection,SecondCollection和IsFirst属性

  • 您是否在所有三个属性上实现了INotifyPropertyChanged?(是的,这里有ObservableCollections无济于事) (2认同)