C# Oxyplot-WPF使绘图视图无效
我正在为我的WPF应用程序使用Oxyplot。我想捕获一些数据,然后我想在图表中显示它们。 我在XAML中有以下代码:C# Oxyplot-WPF使绘图视图无效,c#,wpf,oxyplot,C#,Wpf,Oxyplot,我正在为我的WPF应用程序使用Oxyplot。我想捕获一些数据,然后我想在图表中显示它们。 我在XAML中有以下代码: xmlns:oxy="http://oxyplot.org/wpf" <Window.DataContext> <local:MainViewModel/> </Window.DataContext> <oxy:PlotView Title="{Binding Title}" Margin="241,0,0
xmlns:oxy="http://oxyplot.org/wpf"
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<oxy:PlotView Title="{Binding Title}" Margin="241,0,0,179" Name="Plot1" Grid.Column="2">
<oxy:PlotView.Series>
<oxy:LineSeries ItemsSource="{Binding Points}"/>
</oxy:PlotView.Series>
</oxy:PlotView>
xmlns:oxy=”http://oxyplot.org/wpf"
和类别:
public class MainViewModel
{
/// <summary>
///
/// </summary>
///
public MainViewModel()
{
this.Title = "Sin";
this.Points = new List<DataPoint>();
for (int i = 0; i <= 800; i++)
{
double x = (Math.PI * i) / 400;
double y = Math.Sin(x);
DataPoint p = new DataPoint(x, y);
Points.Add(p);
}
/*this.Points = new List<DataPoint>
{
new DataPoint(0, 4),
new DataPoint(10, 13),
new DataPoint(20, 15),
new DataPoint(30, 16),
new DataPoint(40, 12),
new DataPoint(50, 12)
};*/
}
public string Title { get; private set; }
public IList<DataPoint> Points { get; private set; }
public void Second()
{
this.Points.Clear();
this.Title = "Test";
this.Points = new List<DataPoint>();
for (int i = 0; i <= 800; i++)
{
double x = (Math.PI * i) / 400;
double y = Math.Cos(x);
DataPoint p = new DataPoint(x, 0.5);
Points.Add(p);
}
}
}
public类主视图模型
{
///
///
///
///
公共主视图模型()
{
这个标题是“罪”;
this.Points=新列表();
对于(int i=0;i更改为
这将确保对点的更改传播到图表控件。其次,在ViewModel类中实现INotifyPropertyChanged
IList<DataPoint> _points;
public IList<DataPoint> Points { get{return _points;}; private set{ _points = value; OnPropertyChanged("Points");} }
private void OnPropertyChanged(string p)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
public void Second()
{
this.Points.Clear();
this.Title = "Test";
this.Points = new List<DataPoint>();
for (int i = 0; i <= 800; i++)
{
double x = (Math.PI * i) / 400;
double y = Math.Cos(x);
DataPoint p = new DataPoint(x, 0.5);
Points.Add(p);
}
/* suggested change */
OnPropertyChanged("Points");
}
IList\u点;
公共IList点{get{return{u Points;};私有集{u Points=value;OnPropertyChanged(“点”);}
私有void OnPropertyChanged(字符串p)
{
if(PropertyChanged!=null)
房地产变更(这是新的房地产变更发展(p));
}
第二公共空间()
{
this.Points.Clear();
此.Title=“测试”;
this.Points=新列表();
对于(int i=0;i当前,答案可能会更改,并且已经更改
- 更改PlotView控件的模型特性
- 在PlotView控件上调用Invalidate
- 在PlotModel上调用Invalidate
我建议您删除“PropertyChanged”,并将其插入更新方法中
PlotModel.InvalidatePlot(true);
谢谢,我添加了上面提到的行,并通过以下代码进行了INotify:public class MainViewModel:INotifyPropertyChanged
和public event PropertyChangedEventHandler PropertyChanged;
但是绘图仍然没有刷新。@stanedav绘图正在刷新110%。我已经下载了OxyPlot包并运行了您的示例。您似乎没有我不知道如何正确使用绑定机制。上传了一个示例,请检查它。对不起,我的坏,你是我的英雄:)@stanedav也可以,但它使用双向绑定,这在你的情况下是不需要的。