2016-08-02 60 views
1

我正在制作基于WPFToolkit的图表,我试图在每次点击一个按钮时更新AreaSeries。我在我的数据类上实现了INotifyPropertyChanged。但是当我重新加载数据的源对象ID不图表更新(目标对象)如何在按钮上更新WPFToolkit AreaSeries图表点击

代码如下:

public partial class MainWindow : Window 
{ 

    static List<Ready4LOS> Ready4LOS = new List<Data.Ready4LOS>(); 





    public MainWindow() 
    { 
     InitializeComponent(); 

     chart1.DataContext = Ready4LOS; 
     InitChart(); 
     LoadData(); 
    } 

    private void LoadData() 
    { 
     var path = @"zxzxzxz.log"; 
     Ready4LOS.Clear(); 
     List<APISTATDataModel> daa = APISTATDataModel.GetFromFile(path, new string[] { "|" }, "Ready4TOS"); 

     List<APISTATDataModel> lastn = daa.GetRange(daa.Count - 10, 10); 

     foreach (APISTATDataModel d in lastn) 
     { 
      Ready4LOS.Add(new Ready4LOS() { Case = d.Current_Count, Time = d.Current_Time }); 

     } 

    } 

    private void InitChart() 
    { 
     System.Windows.Data.Binding indi = new System.Windows.Data.Binding("Case"); 
     System.Windows.Data.Binding dep = new System.Windows.Data.Binding("Time"); 
     dep.Mode = System.Windows.Data.BindingMode.OneWay; 
     indi.Mode = System.Windows.Data.BindingMode.OneWay; 
     AreaSeries ares = new AreaSeries(); 
     ares.ItemsSource = Ready4LOS; 
     ares.IndependentValueBinding = dep; 
     ares.DependentValueBinding = indi; 
     ares.Title = "Ready4LOS"; 

     DateTimeAxis dta = new DateTimeAxis(); 
     dta.Interval = 10; 
     dta.IntervalType = DateTimeIntervalType.Minutes; 
     dta.Title = "Time"; 
     dta.Orientation = AxisOrientation.X; 
    // dta.Minimum = DateTime.Now.AddMinutes(-90); 
     // dta.Maximum = DateTime.Now; 

     LinearAxis yaxis = new LinearAxis(); 
     yaxis.Minimum = 0; 
     yaxis.Interval = 2; 
     yaxis.Title = "Case"; 
     yaxis.Orientation = AxisOrientation.Y; 
     yaxis.ShowGridLines = true; 
     chart1.Axes.Add(yaxis); 
     chart1.Axes.Add(dta); 
     chart1.Series.Add(ares); 



    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     LoadData(); 
     chart1.UpdateLayout(); 

    } 



} 

}

数据模型是这里

class Ready4LOS : INotifyPropertyChanged 
{ 
    int _case; 
    DateTime _time; 

    public int Case 
    { 
     get 
     { 
      return _case; 

     } 

     set 
     { 
      _case = value; 
      NotifyPropertyChanged("Case"); 
     } 
    } 

    public DateTime Time 
    { 
     get 
     { 
      return _time; 
     } 

     set 
     { 
      _time = value; 
      NotifyPropertyChanged("Time"); 
     } 
    } 



    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 

当它开始的时候,它完全加载,因为我在开始时调用了LoadData()。 问题是,当我点击刷新按钮时,它会加载源对象中的数据,但目标对象的数据未更新,即图表未更新,它与初始数据保持一致。

回答

1

使用ObservableCollection<Ready4LOS>而不是List<Ready4LOS>ObservableCollection<>已经实现了INotifyPropertyChanged以及INotifyCollectionChanged。如果您要动态更改CaseTime的值,那么您的Ready4LOSINotifyPropertyChanged的实现可能只有在您的集合中已存在Ready4LOS的值时才有必要。

enter image description here

+0

哇,我从来不知道..一定会尝试,让你知道 – rvsingh42

+0

感谢您抽出宝贵时间,帮助我......特地为JIF你把在那里。 – rvsingh42

+0

没问题,我的荣幸。 – jsanalytics