如何将ObservableCollection <lineseries>绑定到Chart </lineseries>

时间:2010-12-20 08:43:47

标签: c# wpf data-binding wpftoolkit

我有图表绑定的问题。 是否可以为WPF工具包折线图提供动态数量的系列?

我发现了许多如何手工制作的例子。

例如,如果我有类似的东西:

    <chartingToolkit:Chart Grid.ColumnSpan="4" Grid.Row="3" HorizontalAlignment="Stretch" 
VerticalAlignment="Stretch" Name="chart_Distribution" Title="Chart Title">

和模型一样:

public class Test : BaseViewModel
{
    private readonly ObservableCollection<LineSeries>  _lineSeries = new ObservableCollection<LineSeries>();                   
    public ObservableCollection<LineSeries> LineSeries
        {
            get { return _lineSeries; }
        }
    private void Test()
        {
            var collection = new ObservableCollection<RequestsInProcess>();
            var collection2 = new ObservableCollection<RequestsInProcess>();

            collection.Add(new RequestsInProcess(0, 4));
            collection.Add(new RequestsInProcess(1, 1));
            collection.Add(new RequestsInProcess(2, 4));
            collection.Add(new RequestsInProcess(3, 3));

            collection2.Add(new RequestsInProcess(0, 3));
            collection2.Add(new RequestsInProcess(1, 2));
            collection2.Add(new RequestsInProcess(2, 2));
            collection2.Add(new RequestsInProcess(3, 1));

            var lineseries1 = new LineSeries();
            lineseries1.IndependentValuePath = "AmountOfRequests";
            lineseries1.DependentValuePath = "Time";
            lineseries1.Title = "Line series1";
            lineseries1.ItemsSource = collection;
            Charts.Add(lineseries1);

            var lineseries2 = new LineSeries();
            lineseries2.IndependentValuePath = "AmountOfRequests";
            lineseries2.DependentValuePath = "Time";
            lineseries2.Title = "Line series2";
            lineseries2.ItemsSource = collection2;
            Charts.Add(lineseries2);

            collection2.Add(new RequestsInProcess(4, 4));
            collection.Add(new RequestsInProcess(4, 4));

        }
}

如何将我的图表绑定到ObservableCollection? 有可能吗?

1 个答案:

答案 0 :(得分:3)