繁体   English   中英

C#线图动态数据绑定

[英]C# linechart dynamically databind

我希望能够为此动态创建图表,我使用名为getChart()的方法创建了以下类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.DataVisualization.Charting;
namespace Henvendelser
{
    class ChartDataCreator
    {
        private Dictionary<String, List<ContactQueue>> dataList;

        public ChartDataCreator() {

        }

        public Chart getChart(String choice) {

            Chart c = new Chart();
            LineSeries ls = new LineSeries();
            ls.IndependentValueBinding="{Binding Path=Key}";
            ls.DependentValueBinding="{Binding Path=Value}";
            ls.ItemsSource =

            new KeyValuePair<int, int>[]{
            new KeyValuePair<int,int>(1, 12),
            new KeyValuePair<int,int>(2, 25),
            new KeyValuePair<int,int>(3, 5),
            new KeyValuePair<int,int>(4, 6),
            new KeyValuePair<int,int>(5, 10),
            new KeyValuePair<int,int>(6, 4),
            new KeyValuePair<int,int>(7, 40),
            new KeyValuePair<int,int>(8, 12),
            new KeyValuePair<int,int>(9, 25),
            new KeyValuePair<int,int>(10, 5),
            new KeyValuePair<int,int>(11, 6),
            new KeyValuePair<int,int>(12, 10),
            new KeyValuePair<int,int>(13, 4),
            new KeyValuePair<int,int>(14, 8),
            new KeyValuePair<int,int>(15, 9),
            new KeyValuePair<int,int>(16, 50),
            new KeyValuePair<int,int>(17, 40) };
            c.Series.Add(ls);
            return c;
        }
    }
}

现在您可以看到我的代码有错误:

ls.IndependentValueBinding="{Binding Path=Key}"; ls.DependentValueBinding="{Binding Path=Value}";

我的问题是如何动态设置独立和独立绑定。 另外,请指出是否还有其他我想念的东西才能使它起作用。

尝试此操作,他们正在寻找绑定对象,因此您必须提供它。

Chart c = new Chart();
LineSeries ls = new LineSeries();
// For new binding you provide it the string path to your property.
Binding bindInd = new Binding("Key");
Binding bindDep = new Binding("Value");
// then you can set the properties of your binding like so
bindInd.Source = <your source>;
bindDep.Source = <Your Source>;
bindDep.Mode = BindingMode.OneWay;
bindInd.Mode = BindingMode.OneWay;
ls.IndependentValueBinding = bindInd;
ls.DependentValueBinding =bindDep;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM