【问题标题】:Programmatically create a ColumnSeries WPF Toolkit Charts以编程方式创建 ColumnSeries WPF 工具包图表
【发布时间】:2010-10-13 16:51:47
【问题描述】:

我正在尝试以编程方式将列系列添加到 wpf 工具包图表。我的 xaml 是一个空图表。该代码导致未处理的异常,对象引用未设置为对象的实例。为什么这不起作用的任何线索?

<charting:Chart Name="MyChart">

我的代码是

List<KeyValuePair<int,int>> testList = new List<KeyValuePair<int,int>>();

testList.Add(new KeyValuePair<int,int> (1,2));

testList.Add(new KeyValuePair<int,int> (2,3)); 

ColumnSeries mySeries = new ColumnSeries();

mySeries.Title = "TEST";


mySeries.IndependentValueBinding = new Binding("key");

mySeries.DependentValueBinding = new Binding("value");

mySeries.ItemsSource = testList;

MyChart.Series.Add(mySeries);

【问题讨论】:

    标签: c# wpf wpftoolkit


    【解决方案1】:

    我也遇到过这个问题,在我将应用程序从 .NET FRAMEWORK 3.5 升级到 4.0 后,图表类突然停止工作。 当我在具有动态列系列图表的表单上调用 Show() 方法时,没有显示新窗口,而是弹出此错误:对象引用未设置为对象的实例。 如果我删除了到 Dictionary 的 itemsource 链接,或者将动态列系列更改为静态 XAML 版本,它虽然可以工作,但是这个静态版本对大多数用户来说是不可用的。

    有人知道如何在 WPF .NET Framework 4.0 中直接实现这一点吗?还是针对 .NET 3.5 的 wpftoolkit 中的错误?

    public void SetChartData(IDictionary<string, IDictionary<string, double>> prod, String title, String labelAxis)
            {   
               chart.Title = title;
               LinearAxis ca = new LinearAxis();
               ca.Orientation = AxisOrientation.Y;
               ca.Minimum = 0;
               chart.Axes.Add(ca);
               foreach (KeyValuePair<string, IDictionary<string, double>> kvp in prod)
               {
                   ColumnSeries cser = new ColumnSeries();
                   cser.Title = kvp.Key;
                 cser.DependentValueBinding = new Binding("Value");
                  cser.IndependentValueBinding = new Binding("Key");
                  cser.ItemsSource = kvp.Value;
                   chart.Series.Add(cser);
               }
            }
    

    我找到了一种可能的解决方法:

    • 为 ex. 创建新的 WPF 项目库。 MyChart,创建一个类,该类将返回带有图表的 WPF 窗口。
    • 设置并将图表库项目编译为 .NET Framework 3.5(客户端)
    • 调用 MyChartClass.Show();在主程序 .NET Framework 4.0 内将正确显示图表

    【讨论】:

      【解决方案2】:

      您应该在绑定中使用“Key”而不是“key”和“Value”而不是“value”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多