【问题标题】:Setting OxyPlot chart title设置 OxyPlot 图表标题
【发布时间】:2018-07-10 12:44:47
【问题描述】:

我正在使用 OxyPlot 在 WPF/.net 4.5 下显示一个基本折线图,XAML 的相关部分如下所示:

<oxy:Plot Title="{Binding Title}">
    <oxy:Plot.Series>
        <oxy:LineSeries ItemsSource="{Binding Points}"/>
        <oxy:LineSeries ItemsSource="{Binding Points2}"/>
    </oxy:Plot.Series>
    <oxy:Plot.Axes>
        <oxy:CategoryAxis Position="Bottom" ItemsSource="{Binding Labels}" />
    </oxy:Plot.Axes>
</oxy:Plot>

创建图表的代码如下:

public class MainViewModel
{

    public MainViewModel()
    {
        this.Title = reader.title;
        this.Points = reader.mylist;
        this.Points2 = reader.mylist2;
        this.Labels = reader.labs;          
    }

    public string Title { get; private set; }
    public IList<DataPoint> Points { get; private set; }
    public IList<DataPoint> Points2 { get; private set; }
    public IList<String> Labels { get; private set; }

}

在另一种方法中根据需要设置和更改属性基本上就像这样:

public static List<DataPoint> mylist = new List<DataPoint>();
public static List<DataPoint> mylist2 = new List<DataPoint>();
public static List<String> labs = new List<String>();
public static string title;

public void setchart()
{

   mylist.Add(new DataPoint(0,5));
   mylist.Add(new DataPoint(1,10));

   mylist2.Add(new DataPoint(0,30));
   mylist2.Add(new DataPoint(1,25));

   labs.Add("date1");
   labs.Add("date2");

  title = "mytitle";

  MainWin.myPlot.InvalidatePlot(true);

}

它适用于图表中的线条 (Points, Points2) 和 x 轴上的标签 (Labels)。但是图表的标题并没有更新——“mytitle”永远不会作为图表的标题出现,除非我这样写

this.Title = "mytitle";

直接进入MainViewModel()

有些人似乎有类似的问题(例如here),但我想知道我是否做错了什么。

【问题讨论】:

    标签: c# wpf data-binding oxyplot


    【解决方案1】:

    在您的 MainViewModel 构造器上,当您将读取器列表分配给 IList 时,两者都将指向同一个对象,因此当您修改一个对象时,另一个也会被修改。

    另一方面,字符串虽然是引用类型,但它是不可变的,所以它的行为很像值类型。因此,当您修改阅读器类中的字符串时,您并没有修改 ViewModel 中的字符串。

    【讨论】:

      猜你喜欢
      • 2015-02-01
      • 2022-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多