Reputation: 307
I want to calculate average etc. on lap data which is stored in a listbox like: XX:XX:XX:XXX but not sure how/what the best approach would be. For example would it be best to put the items in the listbox and an array to calculate from or read the data from the listbox etc.
Thanks
UPDATE: The listbox adds a new entry every time a user presses a button. The information is stored until the app closes.
Upvotes: 0
Views: 159
Reputation: 12465
You'll want to accomplish this through binding. Binding allows you to have data stored in one location and still display it within your UI. Binding is the basis for the MVVM (Model View ViewModel) framework.
Here is an example of what your XAML would look like
<ListBox ItemsSource="{Binding LapTimes}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Time}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Your code for your page could look something like
public MainPage()
{
DataContext = new MainViewModel();
InitializeComponent();
}
private void AddButtonClick(object sender, EventArgs e)
{
TimeSpan time = // get time
((MainViewModel)DataContext).LapTimes.Add(time);
}
And the backing ViewModel could look like
public class MainViewModel // Should implement INotifyPropertyChange
{
public MainViewModel()
{
LapTimes = new ObservableCollection<TimeSpan>();
}
public ObservableCollection<TimeSpan> LapTimes { get; private set; }
private void CalculateStuff()
{
// calculate times
}
}
Upvotes: 1
Reputation: 521
You should use a list to hold the data, and you can then bind listbox to the data. You perform calculations that you require using linq.
See here for examples: http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
Upvotes: 1