如何将多个系列数据绑定到Silverilght工具包图表

时间:2022-10-01 12:32:59

I am trying to use databinding to bind data to a Silverlight toolkit chart. I will have one to many sets of series so cannot determine how many series i need before hand. I also want to stick to a databinding model and not resort to programmatically adding these series as many other controls bind to this datasource.

我正在尝试使用数据绑定将数据绑定到Silverlight工具包图表。我会有一到多套系列,所以无法确定我需要的系列数量。我还想坚持数据绑定模型,而不是通过编程方式添加这些系列,因为许多其他控件绑定到此数据源。

I found an article on the web by Jeremiah Morrill that showed a solution for this very problem.

我在网上发现了Jeremiah Morrill的一篇文章,该文章展示了解决这个问题的方法。

Jeremiah's solution

Now this worked perfectly at first, until I tried to update my databinding's datasource values while the application was running, and this would not reflect. As if it was not subscribed to the PropertyChanged event. I even bound the same data to a datagrid next to the chart, and the datagrid reacts as expected changing everytime my databinding's datasource values change.

现在这首先完美地工作,直到我在应用程序运行时尝试更新我的数据绑定的数据源值,这不会反映出来。好像它没有订阅PropertyChanged事件。我甚至将相同的数据绑定到图表旁边的数据网格,并且每当我的数据绑定的数据源值发生变化时,datagrid会按预期进行更改。

In my ChartHelper from Jeremiah's solution, i have the following dependency property

在Jeremiah解决方案的ChartHelper中,我有以下依赖属性

public static readonly DependencyProperty SeriesSourceProperty =
        DependencyProperty.RegisterAttached("SeriesSource", 
                                            typeof(IEnumerable), 
                                            typeof(ChartHelper),
                                            new PropertyMetadata(SeriesSourceChanged));

The SeriesSourceChanged event is called when my application starts up. However, when my DataBinding's datasource values change, this is not called again.

我的应用程序启动时会调用SeriesSourceChanged事件。但是,当我的DataBinding的数据源值发生更改时,不会再次调用它。

So questions are as follows:

所以问题如下:

  • How can I capture the PropertyChanged notification with this solution?
  • 如何使用此解决方案捕获PropertyChanged通知?

  • Is there something I can add to the DependencyProperty above to capture this?
  • 有什么东西可以添加到上面的DependencyProperty来捕获它吗?

  • Is it something i need to add to the chart or series to achieve this?
  • 是否需要添加到图表或系列中以实现此目的?

I have been racking my brain over this for several days, so any help or suggestions will be much appreciated

几天来,我一直绞尽脑汁,所以任何帮助或建议都会受到赞赏

Thanks!

2 个解决方案

#1


The SeriesSource type should be ObservableCollection instead of IEnumerable. Then you do something like this:

SeriesSource类型应该是ObservableCollection而不是IEnumerable。然后你做这样的事情:

private static void SeriesSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
    var sender = o as YourType;
    var newCollection = e.NewValue as ObservableCollection<DataSetViewModel>;
    if (newCollection != null)
    {
        newCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(sender.OnCollectionChanged);
    }
}

private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
}

#2


I never did find a solution to this problem and ended up using a chart control from visifire

我从未找到解决此问题的方法,最终使用了visifire的图表控件

I found this much easier to customise but never found a neat way of using databinding to achieve this and ended up with a more programattic approach.

我发现这更容易定制,但从来没有找到一种使用数据绑定来实现这一目标的简洁方法,最终得到了更加专业化的方法。

#1


The SeriesSource type should be ObservableCollection instead of IEnumerable. Then you do something like this:

SeriesSource类型应该是ObservableCollection而不是IEnumerable。然后你做这样的事情:

private static void SeriesSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
    var sender = o as YourType;
    var newCollection = e.NewValue as ObservableCollection<DataSetViewModel>;
    if (newCollection != null)
    {
        newCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(sender.OnCollectionChanged);
    }
}

private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
}

#2


I never did find a solution to this problem and ended up using a chart control from visifire

我从未找到解决此问题的方法,最终使用了visifire的图表控件

I found this much easier to customise but never found a neat way of using databinding to achieve this and ended up with a more programattic approach.

我发现这更容易定制,但从来没有找到一种使用数据绑定来实现这一目标的简洁方法,最终得到了更加专业化的方法。