使用INotifyPropertyChanged接口

时间:2015-08-19 14:06:44

标签: c# winforms charts inotifypropertychanged

我正在尝试实现一个winform应用程序,该应用程序在线图上显示一组样本(来自HW设备)。

我使用了INotifyPropertyChanged接口并将图表绑定到HW设备的模型,但似乎在HW模型中更改样本时图表不会更新。

很抱歉,如果这太基础了(我更像是一个嵌入式人员),但似乎我错过了将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
    }
}

在HW模型中,我编写了以下代码来实现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;
    }
}

2 个答案:

答案 0 :(得分:0)

ADCSamples根本没有实现IOnNotifyPropertyChanged

你可以:

  • 将其更改为索引器属性并正确实施IOnNotifyPropertyChanged PropertyChanged for indexer property

  • 将其更改为已实施ObservableCollection的{​​{1}}:

    公共类GSWatchModel:INotifyPropertyChanged {     公共事件PropertyChangedEventHandler PropertyChanged;     private void NotifyPropertyChanged([CallerMemberName] String propertyName =“”)     {         if(PropertyChanged!= null)         {             PropertyChanged(this,new PropertyChangedEventArgs(propertyName));         }     }

    IOnNotifyPropertyChanged

    }

    公共部分类Form1:表单 {     公共Form1()     {         的InitializeComponent();         StreamChart.Series [0] .Points.DataBindY(GSWatch.ADCSamples);     }

    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;
    }
    

    }

答案 1 :(得分:0)

在绑定之前移动StartStreamADC ...见下文:

    private void Form1_Load(object sender, EventArgs e)
    {
        GSWatchModel GSWatch = new GSWatchModel();
        GSWatch.StartStreamADC();

        StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
    }

结果:

enter image description here

要获得通知,请执行以下操作:

    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>();

它会为你省去很多麻烦。