开发者

Switching Binding Sources using a MultiBinding

I have a DataBinding with a MultiBinding of two ObservableCollections, and i want to switch between them on a condition with a MultiConv开发者_C百科erter. So the converter gives the right collection, but the binding doesn't seem to be updated.

Any Ideas??

Greets,

Jürgen


This is the converter you need:

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();
    }
}

registering the converter:

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

usage of the binding:

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

under the assumption that you have a FirstCollection, a SecondCollection, and an IsFirst Properties in the DataContext


Do you need the view to update the source lists?

If so, your binding should be in TwoWay mode:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜