【发布时间】:2015-08-19 14:06:44
【问题描述】:
我正在尝试实现一个 winform 应用程序,它在折线图上显示一组样本(来自硬件设备)。
我使用了 INotifyPropertyChanged 接口并将图表绑定到硬件设备的模型,但是在硬件模型处更改样本时,图表似乎没有更新。
对不起,如果这太基本了(我更像是一个嵌入式的人),但似乎我缺少将 INotifyPropertyChanged 事件连接到数据绑定器的部分。
这里有什么遗漏吗?还是我应该以不同的方式实现它?
在 WinForm 类中,我编写了以下代码来将图表绑定到 HW 模型的示例 当 'ADCSamples' 发生变化时,按钮应显示大小写:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
}
private GSWatchModel GSWatch = new GSWatchModel();
private void button1_Click(object sender, EventArgs e)
{
uint[] muki = new uint[128];
for (int i = 0; i < 128; i++)
{
muki[i] = (uint)(i / 10);
}
GSWatch.ADCSamples = muki;
//StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples); //The chart is only updated if this line is executed
}
private void button2_Click(object sender, EventArgs e)
{
GSWatch.StartStreamADC();
//StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples); //The chart is only updated if this line is executed
}
}
在硬件模型中,我编写了以下代码来实现 INotifyPropertyChanged 功能:
public class GSWatchModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private uint[] aDCSamples = new uint[128];
public uint[] ADCSamples
{
get
{
return aDCSamples;
}
set
{
aDCSamples = value;
NotifyPropertyChanged();
}
}
public GSWatchModel()
{
CommLink = new GSCommManager();
for (int i = 0; i < 128; i++)
{
aDCSamples[i] = (uint)(i); //initial values for demo
}
}
uint muki = 0;
public void StartStreamADC()
{
GSPacket StreamRequestPacket = new GSPacket(GSPTypes.Stream);
CommLink.SendViaGSWatchLink(StreamRequestPacket);
for (int i = 0; i < 128; i++)
{
aDCSamples[i] = (uint)i / 10; //steps for demonstration
}
NotifyPropertyChanged();
muki += 100;
}
}
【问题讨论】:
标签: c# winforms charts inotifypropertychanged