使用 INotifyPropertyChanged 接口绑定 Winform 图表
Binding Winform Chart using INotifyPropertyChanged Interface
我正在尝试实现一个 winform 应用程序,该应用程序在折线图上显示一组样本(来自硬件设备)。
我使用了 INotifyPropertyChanged 接口并将图表绑定到 HW 设备的模型,但是当在 HW 模型上更改示例时图表似乎没有更新。
抱歉,如果这太基础了(我更像是一个嵌入式的人),但我似乎遗漏了将 INotifyPropertyChanged 事件连接到数据绑定器的部分。
这里是不是少了什么?还是我应该以不同的方式实施?
在 WinForm class 我编写了以下代码将图表绑定到 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;
}
}
ADCSamples
根本没有实现 IOnNotifyPropertyChanged
。
你可以:
将其更改为索引器 属性 并正确实施 IOnNotifyPropertyChanged
PropertyChanged for indexer property
将其更改为 ObservableCollection
已经实现了 IOnNotifyPropertyChanged
:
public class GSWatchModel:INotifyPropertyChanged
{
public 事件 PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String 属性Name = "")
{
如果(PropertyChanged!= null)
{
PropertyChanged(这个,新的 PropertyChangedEventArgs(属性Name));
}
}
private ObservableCollection<uint> aDCSamples = new ObservableCollection<uint>();
public ObservableCollection<uint> ADCSamples
{
get
{
return aDCSamples;
}
set
{
aDCSamples = value;
NotifyPropertyChanged("ADCSamples");
}
}
public GSWatchModel()
{
CommLink = new GSCommManager();
for (int i = 0; i < 128; i++)
{
ADCSamples.Add((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
}
muki += 100;
}
}
public 部分 class Form1 : 表格
{
public 表格 1()
{
初始化组件();
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
}
private GSWatchModel GSWatch = new GSWatchModel();
private void button2_Click(object sender, EventArgs e)
{
GSWatch.StartStreamADC();
}
}
在绑定之前移动StartStreamADC
...见下文:
private void Form1_Load(object sender, EventArgs e)
{
GSWatchModel GSWatch = new GSWatchModel();
GSWatch.StartStreamADC();
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
}
结果:
要获得通知,请执行以下操作:
private void Form1_Load(object sender, EventArgs e)
{
GSWatch = new GSWatchModel();
GSWatch.StartStreamADC();
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
GSWatch.PropertyChanged += GSWatch_PropertyChanged;
}
private void GSWatch_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
}
另外,请将ADCSamples
改为:
public List<uint> ADCSamples = new List<uint>();
它会让你省去很多麻烦。
我正在尝试实现一个 winform 应用程序,该应用程序在折线图上显示一组样本(来自硬件设备)。
我使用了 INotifyPropertyChanged 接口并将图表绑定到 HW 设备的模型,但是当在 HW 模型上更改示例时图表似乎没有更新。
抱歉,如果这太基础了(我更像是一个嵌入式的人),但我似乎遗漏了将 INotifyPropertyChanged 事件连接到数据绑定器的部分。
这里是不是少了什么?还是我应该以不同的方式实施?
在 WinForm class 我编写了以下代码将图表绑定到 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;
}
}
ADCSamples
根本没有实现 IOnNotifyPropertyChanged
。
你可以:
将其更改为索引器 属性 并正确实施
IOnNotifyPropertyChanged
PropertyChanged for indexer property将其更改为
ObservableCollection
已经实现了IOnNotifyPropertyChanged
:public class GSWatchModel:INotifyPropertyChanged { public 事件 PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] String 属性Name = "") { 如果(PropertyChanged!= null) { PropertyChanged(这个,新的 PropertyChangedEventArgs(属性Name)); } }
private ObservableCollection<uint> aDCSamples = new ObservableCollection<uint>(); public ObservableCollection<uint> ADCSamples { get { return aDCSamples; } set { aDCSamples = value; NotifyPropertyChanged("ADCSamples"); } } public GSWatchModel() { CommLink = new GSCommManager(); for (int i = 0; i < 128; i++) { ADCSamples.Add((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 } muki += 100; }
}
public 部分 class Form1 : 表格 { public 表格 1() { 初始化组件(); StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples); }
private GSWatchModel GSWatch = new GSWatchModel(); private void button2_Click(object sender, EventArgs e) { GSWatch.StartStreamADC(); }
}
在绑定之前移动StartStreamADC
...见下文:
private void Form1_Load(object sender, EventArgs e)
{
GSWatchModel GSWatch = new GSWatchModel();
GSWatch.StartStreamADC();
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
}
结果:
要获得通知,请执行以下操作:
private void Form1_Load(object sender, EventArgs e)
{
GSWatch = new GSWatchModel();
GSWatch.StartStreamADC();
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
GSWatch.PropertyChanged += GSWatch_PropertyChanged;
}
private void GSWatch_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
}
另外,请将ADCSamples
改为:
public List<uint> ADCSamples = new List<uint>();
它会让你省去很多麻烦。