Maintaining 60 elements in a observablecollection

Oct 13, 2011 at 1:32 PM

Hi I have a ObservableCollection getting data in every minute. When it reaches an hour, I would like to clear the first item and move all the items up then adding the new item, thus maintaining it at 60 elements. Does anyone have any idea how to do so?

Here is my code:

public class MainWindow : Window

{

    double i = 0;
   
double SolarCellPower = 0;
     
DispatcherTimer timer = new DispatcherTimer();

ObservableCollection> Power = new ObservableCollection>();

    public MainWindow()


   
{
       
InitializeComponent();


         timer
.Interval = new TimeSpan(0, 0, 1);  // per 5 seconds, you could change it
        timer
.Tick += new EventHandler(timer_Tick);
        timer
.IsEnabled = true;
   
}

   
void timer_Tick(object sender, EventArgs e)
   
{
       
SolarCellPower = double.Parse(textBox18.Text);
       
Power.Add(new KeyValuePair<double, double>(i, SolarCellPower ));
        i
+= 5;
       
Solar.ItemsSource = Power;
   
}

}

Oct 14, 2011 at 1:32 PM

You might have a look at the Queue class (http://msdn.microsoft.com/en-us/library/7977ey2c.aspx). It works with an automatic FIFO system (First In, First Out). It is not Observable though, so you might have to subclass it and implement the INotifyCollectionChanged and/or INotifyPropertyChanged interfaces.

I don't think it has indexed access to each item, but I don't know if that is a requirement for you.

Marcel