【发布时间】:2019-07-27 00:24:56
【问题描述】:
WPF 新手,不知道如何以编程方式实例化包装新图表及其数据集合的新视图模型。目前,它包含以下内容,但不确定设置它的最佳方式。
class ChartViewModel
{
public ChartViewModel()
{
CartesianChart chart = new CartesianChart();
chart.Series = new SeriesCollection
{
new GLineSeries
{
Title = "Pressure",
Values = new GearedValues<double>(),
},
new GLineSeries
{
Title = "Pulse",
Values = new GearedValues<int>(),
}
};
}
}
然后,我需要将新图表添加到视图中。 CartesianChart 对象是 UIElement,当我在没有此类的情况下在主窗口中对其进行测试时,它的工作原理如下。
stackPanel.Children.Add(chart);
但是该类似乎无法访问 xaml,并且我无法添加实际的视图模型类,因为那不是 UIElement,只有图表是。基本上每次上一个图表填满这样的东西时都需要创建一个新的图表实例:
ChartViewModel tempChart = new ChartViewModel();
chartRepo.Add(tempChart); //chart repo is a list of ChartViewModels
所以它需要自己的 SeriesCollection 和 UIElement。感谢您的任何建议。
【问题讨论】:
标签: wpf livecharts