【问题标题】:Binding Winform Chart using INotifyPropertyChanged Interface使用 INotifyPropertyChanged 接口绑定 Winform 图表
【发布时间】: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


    【解决方案1】:

    在绑定之前移动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>();
    

    这样可以省去很多麻烦。

    【讨论】:

    • 非常感谢。当我在绑定之前 StartStremADC 时,它确实显示在图表上,但无论何时更改样本,图表都不会改变。正如 daniel-luberda 所说,我可能没有正确实施 IOnNotifyPropertyChanged。你能告诉我在这个例子中我应该如何正确实现 IOnNotifyPropertyChanged 吗?
    • 每当您的数据样本发生变化时,您都必须致电chart.DataBind()INotifyPropertyChanged 的实现对我来说看起来是正确的......唯一的问题是,我通常只将它与 WPF 应用程序一起使用,而不是 WinForms。在 WinForms 中,我通常只调用 DataBind()
    • 另外,您没有发布更改示例数据的代码,仅发布了初始数据加载。如果数据更改时遇到问题,则必须发布那段代码。
    • 问题是当样本发生变化时我不能调用'chart.DataBind()',因为它们在模型而不是表单上发生了变化。这就是为什么我想首先使用“INotifyPropertyChanged”。我已经更新了我的问题代码。我需要每当调用“StartStreamADC()”或调用“ADCSamples”设置器时,图表都会被更新。但事实并非如此。
    • 非常感谢。这正是我所缺少的。
    【解决方案2】:

    ADCSamples 根本没有实现 IOnNotifyPropertyChanged

    你可以:

    • 将其更改为索引器属性并正确实现IOnNotifyPropertyChangedPropertyChanged for indexer property

    • 将其更改为已实现 IOnNotifyPropertyChangedObservableCollection

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

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

      }

      公共部分类 Form1 : Form { 公共表格1() { 初始化组件(); StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples); }

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

      }

    【讨论】:

    • 非常感谢您的提示。我想我没有正确实现它。我不确定我是否正确理解如何在我的示例中实现它。您能否详细说明我在表单部分缺少的内容?
    • 我更新了例子(不知道为什么代码格式不正确)
    • 非常感谢 ObservableCollection 的实施。我已经尝试过了,但它仍然没有得到更新,因为@jstreet sugested 的事件捕获功能丢失了,而且 'StartStreamADC()' 中也缺少了 'NotifyPropertyChanged("ADCSamples")'。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 2014-02-01
    • 2023-03-13
    • 2018-08-31
    相关资源
    最近更新 更多