如何绑定 ObservableCollection到图表

发布于 2024-10-08 14:09:06 字数 2080 浏览 3 评论 0原文

我在图表绑定方面遇到了一个小问题。 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? 是否可以?

I have a litle problem with Chart binding.
Is it possible to have a dynamic number of series for a WPF toolkit line chart?

I found many examples how to do it manualy.

For example If I have something like:

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

And Model like:

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

        }
}

How can I Bind my chart to ObservableCollection?
Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文