以编程方式创建 ColumnSeries WPF 工具包图表

发布于 2024-09-27 15:33:52 字数 659 浏览 0 评论 0原文

我正在尝试以编程方式将列系列添加到 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);

I am trying to programmatically add a column series to a wpf toolkit chart. My xaml is an empty chart. The code results in an unhandled exception, Object reference not set to an instance of an object. Any clues to why this does not work?

<charting:Chart Name="MyChart">

my code behind is

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

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

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

发布评论

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

评论(2

折戟 2024-10-04 15:33:52

我也遇到了这个问题,在我将应用程序从 .NET FRAMEWORK 3.5 升级到 4.0 后,图表类突然停止工作。
当我在具有动态列系列图表的表单上调用 Show() 方法时,而不是显示新窗口,而是弹出此错误:对象引用未设置到对象的实例。
如果我删除到 Dictionary 的 itemsource 链接,或将动态 columnseries 更改为静态 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);
           }
        }

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

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

I have come to this problem too, after i have upgraded my application from .NET FRAMEWORK 3.5 to 4.0, suddently the chart class stopped working.
When i called Show() method on form that had the chart with dynamic columnseries, instead of displaying the new window, this error popped up: Object reference not set to an instance of an object.
If i remove the itemsource link to Dictionary, or change the dynamic columnseries to static XAML version, it works though, but this static version is unusable for most users.

anybody has idea how to implement this directly in WPF .NET Framework 4.0? or its bug in wpftoolkit which is targetted to .NET 3.5 ?

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

i have found one possible workaround:

  • create new WPF project library for ex. MyChart, create a class that will return WPF Window with chart inside.
  • setup and compile the chart library project as .NET Framework 3.5 (client)
  • calling MyChartClass.Show(); inside main program .NET Framework 4.0 will display the chart properly
凶凌 2024-10-04 15:33:52

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

You should use "Key" instead of "key" and "Value" instead of "value" in the binding.

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