可以在c# -WPF图表控件中自定义标记样式

本文关键字:自定义 样式 控件 -WPF | 更新日期: 2023-09-27 18:04:00

我是c#新手。使用图表,并试图从我绘制的线条系列中删除标记。

下面是代码行:

private void LoadLineChartData()
    {
        LineSeries ls1 = new LineSeries();
        ls1.Title = "Title1";
        ls1.IndependentValueBinding = new Binding("Key");
        ls1.DependentValueBinding = new Binding("Value");
        ls1.ItemsSource =
                    new KeyValuePair<int, int>[]{
    new KeyValuePair<int,int>(1, 100),
    new KeyValuePair<int,int>(2, 130),
    new KeyValuePair<int,int>(3, 150),
    new KeyValuePair<int,int>(4, 125),
    new KeyValuePair<int,int>(5,155) };
        MyChart.Series.Add(ls1);
        ls1.MarkerStyle = MarkerStyle.None;        
    }

它不工作,这里是错误:"System.Windows.Controls.DataVisualization.Charting。LineSeries'不包含'MarkerStyle'的定义,也没有扩展方法'MarkerStyle'接受类型为'

的第一个参数

我是否使用错误的。dll作为图表的参考?什么是正确的?

可以在c# -WPF图表控件中自定义标记样式

当你应该引用System.Windows.Forms时,你却引用了System.Windows.Controls .

在你的项目中,右键单击"References"并添加一个引用到:System.Windows.Forms和system . windows . form . datavuvisualization

在引用了正确的程序集之后,将代码更改为如下内容:

// Declare the following usings
using System.Windows;
using System.Windows.Forms.DataVisualization.Charting;
using System.Collections.Generic;
...
    private void LoadLineChartData()
    {
        Chart myChart = new Chart();
        myChart.Series.Add("ls1");
        myChart.Series["ls1"].ChartType = SeriesChartType.Line;
        myChart.Series["ls1"].MarkerStyle = MarkerStyle.None;
        KeyValuePair<int, int>[] pairs =
        {
            new KeyValuePair<int, int>(1, 100),
            new KeyValuePair<int, int>(2, 130),
            new KeyValuePair<int, int>(3, 150),
            new KeyValuePair<int, int>(4, 125),
            new KeyValuePair<int, int>(5, 155)
        };
        foreach (var pair in pairs)
            myChart.Series["ls1"].Points.AddXY(pair.Key, pair.Value);
    }