Wpf 使用多重绑定切换绑定源

Wpf 使用多重绑定切换绑定源,wpf,data-binding,observablecollection,Wpf,Data Binding,Observablecollection,我有一个数据绑定,有两个可观察集合的多重绑定,我想在多转换器的条件下在它们之间切换。 因此转换器提供了正确的集合,但绑定似乎没有更新 有什么想法吗 问候 Jürgen这是您需要的转换器: public class SwitchCollectionsConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Global

我有一个数据绑定,有两个可观察集合的多重绑定,我想在多转换器的条件下在它们之间切换。 因此转换器提供了正确的集合,但绑定似乎没有更新

有什么想法吗

问候


Jürgen

这是您需要的转换器:

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();
    }
}
注册转换器:

    <local:SwitchCollectionsConverter x:Key="TheConverter" />

绑定的用法:

    <ItemsControl>
        <ItemsControl.ItemsSource>
            <MultiBinding Converter="{StaticResource TheConverter}">
                <Binding Path="FirstCollection" />
                <Binding Path="SecondCollection" />
                <Binding Path="IsFirst" />
            </MultiBinding>
        </ItemsControl.ItemsSource>
    </ItemsControl>


假设DataContext中有FirstCollection、SecondCollection和IsFirst属性,这就是您需要的转换器:

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();
    }
}
注册转换器:

    <local:SwitchCollectionsConverter x:Key="TheConverter" />

绑定的用法:

    <ItemsControl>
        <ItemsControl.ItemsSource>
            <MultiBinding Converter="{StaticResource TheConverter}">
                <Binding Path="FirstCollection" />
                <Binding Path="SecondCollection" />
                <Binding Path="IsFirst" />
            </MultiBinding>
        </ItemsControl.ItemsSource>
    </ItemsControl>


假设您在DataContext中有一个FirstCollection、一个SecondCollection和一个IsFirst属性,您是否需要该视图来更新源列表

如果是这样,您的绑定应处于双向模式:

<TextBox Text="{Binding Source, Mode="TwoWay"}" />

是否需要该视图来更新源列表

如果是这样,您的绑定应处于双向模式:

<TextBox Text="{Binding Source, Mode="TwoWay"}" />


这就是我所拥有的,它似乎正在工作,但如果原始绑定被多重绑定中的另一个绑定替换,我的视图似乎不会更新。我总是在树中显示旧数据…您是否在所有三个属性上都实现了INotifyPropertyChanged?(是的,在这里有可观察的收集是没有帮助的)我认为这与此无关。假设原始绑定在FirstCollection上,并将更改为SecondCollection,因此Bing将更改,而不是集合本身。。。还是我错了?我会被诅咒的,它真的有用。。。。Suoulda在重播之前已经试过了…;)。干得好!这就是我所拥有的,它似乎正在工作,但是如果原始绑定被多绑定中的另一个替换,我的视图似乎不会更新。我总是在树中显示旧数据…您是否在所有三个属性上都实现了INotifyPropertyChanged?(是的,在这里有可观察的收集是没有帮助的)我认为这与此无关。假设原始绑定在FirstCollection上,并将更改为SecondCollection,因此Bing将更改,而不是集合本身。。。还是我错了?我会被诅咒的,它真的有用。。。。Suoulda在重播之前已经试过了…;)。干得好!