0

I have MainWindow class on which im showing realtime chart that is specified in DataChart class. Now when I run my app, chart will start adding new data and refreshing, because I start new thread for this in constructor of DataChart class. But what I need is to start updating chart AFTER I click button defined in MainWindow class, not after app start. But when I start same Thred from MainWindow, chart does not update and PropertyChangedEventHandler is null.

In MainWindow:

private void connectBtn_Click(object sender, RoutedEventArgs e)
        {
            DataChart chart = new DataChart();
            Thread thread = new Thread(chart.AddPoints);
            thread.Start();
        }

In DataChart:

public class DataChart : INotifyPropertyChanged
    {
        public DataChart()
        {
            DataPlot = new PlotModel();

            DataPlot.Series.Add(new LineSeries
            {
                Title = "1",
                Points = new List<IDataPoint>()
            });
            m_userInterfaceDispatcher = Dispatcher.CurrentDispatcher;
            //WHEN I START THREAD HERE IT WORKS AND PROPERTYCHANGED IS NOT NULL
            //var thread = new Thread(AddPoints);
            //thread.Start();                     
        }

        public void AddPoints()
        {
            var addPoints = true;
            while (addPoints)
            {
                try
                {
                    m_userInterfaceDispatcher.Invoke(() =>
                    {
                        (DataPlot.Series[0] as LineSeries).Points.Add(new DataPoint(xvalue,yvalue));
                        if (PropertyChanged != null) //=NULL WHEN CALLING FROM MainWindow
                        {
                            DataPlot.InvalidatePlot(true);
                        }
                    });
                }
                catch (TaskCanceledException)
                {
                    addPoints = false;
                }
            }
        }
        public PlotModel DataPlot
        {
            get;
            set;
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private Dispatcher m_userInterfaceDispatcher;
    }

I think the problem why chart is not updating is that PropertyChanged=null, but i cant figure out how to solve it. Im using OxyPlot if it helps.

MainWindow.xaml:

<oxy:Plot Model="{Binding DataPlot}" Margin="10,10,10,10" Grid.Row="1" Grid.Column="1"/>
BblackK
  • 524
  • 1
  • 8
  • 25
  • There is no code/XAML that causes any binding that would populate your PropertyChanged event. What or who is "listening" to your property? – Nathan A Apr 17 '14 at 17:40
  • 1
    I don't see where you subscribed the `PropertyChanged` event? I believe you data bound it, but that should be different instance. – Sriram Sakthivel Apr 17 '14 at 17:41
  • I have added MainWindow.xaml code. – BblackK Apr 17 '14 at 17:44
  • What is MainWindow's DataContext? – Sriram Sakthivel Apr 17 '14 at 17:48
  • How did the code look which you used during app start for setup/initialization of the DataChart? Since it worked then, as you said, seeing this code might give a hint about what is wrong and why some data binding is not working anymore... –  Apr 17 '14 at 17:52
  • I had `` in `` or in ``. Chart works the same in both cases. – BblackK Apr 17 '14 at 18:00
  • During app start its two rows that i have put in comment `var thread = new Thread(AddPoints);` and `thread.Start();` . But when I start this thread from another class on button click it doesnt work (chart not updating). – BblackK Apr 17 '14 at 18:05

1 Answers1

0

Your problem is you are creating new instance of DataChart as local variable. How you you expect data binding would have subscribed its event?

DataBinding will have subscribed the event of instance which was set as the DataContext, so you need to call AddPoints on the same instance. Try the following:

private void connectBtn_Click(object sender, RoutedEventArgs e)
{
    DataChart chart = (DataChart)this.DataContext;
    Thread thread = new Thread(chart.AddPoints);
    thread.Start();
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • ´PropertyChanged´ in ´DataChart´ class has still null value (the chart not updating) when running thread, but ill try to work with this. – BblackK Apr 17 '14 at 21:06