绑定到多个数据源 [英] Binding to multiple sources

查看:195
本文介绍了绑定到多个数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我密谋与 D3 图。在这个时刻,我通过这样的方式结合做:

I'm plotting graphs with D3. At this moment I'm doing it by binding this way:

<d3:ChartPlotter x:Name="plotter" ItemsSource="Charts" Margin="20">

和我添加/删除项目图表和绘图仪自动更新。工作得很好。

And I Add/Remove items to Charts and the plotter updates automatically. Works very well.

问题是,我需要从多个集合绑定,但显然我不能设置的ItemsSource两次。我读过一些关于CompositeCollections,但几乎每一篇文章是基于一个静态资源,那不是我的情况。

The problem is that I need to bind from several collections, but obviously I can't set ItemsSource twice. I've read something about CompositeCollections, but almost every article is based on a StaticResource, that is not my case.

 <d3:ChartPlotter x:Name="plotter"Margin="20">
 <d3:ChartPlotter.ItemsSource>
    <CompositeCollection>
      <CollectionContainer Collection="{Binding Charts}" />
      <CollectionContainer Collection="{Binding Charts2}" />
      </CompositeCollection>
  </d3:ChartPlotter.ItemsSource>

这code编译,但绑定不起作用。

This code compiles but the binding doesn't work.

我已经搜查了很多,但令人惊讶我还没有找到答案。我认为这是hadto在WPF的共同任务。

I've searched a lot but surprisingly I have not found the answer. I thought this hadto be a common task in WPF.

我打开绑定多个集合到一个单一的ItemsSource,而手动添加的每个项目每个子集到图表我认为这是太麻烦了其他方式。谢谢。

I'm open to other ways of binding multiple collections to a single ItemsSource, but adding manually each item from each sub collection to Charts I think it's too troublesome. Thank you.

编辑:

我通过MultiBinding试图做到这一点,这是转换

I'm trying do it via MultiBinding and this is the scheme of the Converter

EDIT2:

图表是的ObservableCollection&LT;线图&GT;

public class ConcatConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        ObservableCollection<LineGraph> enumerables = new ObservableCollection<LineGraph>();

        foreach (LineGraph line in values[0])
        {
            enumerables.Add(line);
        }

        foreach (LineGraph line in values[1])
        {
            enumerables.Add(line);
        }

        return enumerables;

    }
    public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

由于这个错误,我不能编译:对象foreach语句大炮类型的变量操作,因为对象不包含公共定义的GetEnumerator

I can't compile because of this error: "foreach statement cannon operate in variables of type "object" because "object" does not contain public definition for "GetEnumerator".

推荐答案

使用的 MultiBinding 。首先,做一个转换器,将你想要做什么:

Use a MultiBinding. First, make a converter that will do what you want:

public class ConcatConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        List<IEnumerable> enumerables = new List<IEnumerable>();
        foreach (object obj in values)
        {
            IEnumerable temp = obj as IEnumerable;
            if (temp == null) throw new ArgumentException();
            enumerables.Add(temp);
        }
        List<dynamic> enDynamic = new List<dynamic>();
        enDynamic.AddRange(enumerables);
        return Concat((dynamic)enDynamic);
    }
    private IEnumerable<T> Concat<T>(params IEnumerable<T>[] toConcat)
    {
        return toConcat.Aggregate((a, b) => a.Concat(b));
    }
    private IEnumerable Concat(params IEnumerable[] toConcat)
    {
        ArrayList temp = new ArrayList();
        foreach (IEnumerable x in toConcat)
        {
            foreach (object n in x)
            {
                temp.Add(n);
            }
        }
        return temp;
    }
    public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

(非通用,但我并不想用钝金额反射)

(non-generic but I didn't want to use obtuse amounts of reflection)

然后把它添加到你的窗口资源:

Then add it to your window's resources:

<!--in your window declaration-->
xmlns:local="clr-namespace:YourNameSpace"
<!--after that-->
<Window.Resources>
    <ttp:ConcatConverter x:Key="Concat"/>
</Window.Resources>
<!--finally:-->
<d3:ChartPlotter.ItemsSource>
    <MultiBinding Converter="{StaticResource ResourceKey=Concat}">    
        <Binding Source="Charts"/>
        <Binding Source="Charts2"/>
    </MultiBinding>
</d3:ChartPlotter.ItemsSource>

这篇关于绑定到多个数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆