-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathMainViewModel.cs
76 lines (69 loc) · 1.97 KB
/
MainViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.ObjectModel;
using System.Windows.Navigation;
using System.Windows.Threading;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
namespace PreserveSelectedItemScrollPosition
{
public class MainViewModel : ViewModelBase
{
private ObservableCollection<Club> clubs;
private Club selectedItem;
private static Random random = new Random();
private DispatcherTimer timer;
private int counter = 1;
public MainViewModel()
{
this.Clubs = Club.GetClubs();
this.timer = new DispatcherTimer();
this.timer.Interval = TimeSpan.FromSeconds(0.1);
this.timer.Tick += (s, e) =>
{
this.AddItem();
this.RemoveItem();
};
this.timer.Start();
}
private void AddItem()
{
var index = random.Next(0, this.Clubs.Count);
this.Clubs.Insert(index, new Club("New Club " + counter++, DateTime.Now, 500));
}
private void RemoveItem()
{
var index = random.Next(0, this.Clubs.Count);
if (index != this.Clubs.IndexOf(this.SelectedItem))
{
this.Clubs.RemoveAt(index);
}
}
public ObservableCollection<Club> Clubs
{
get
{
return this.clubs;
}
set
{
if (this.clubs != value)
{
this.clubs = value;
this.OnPropertyChanged("Clubs");
}
}
}
public Club SelectedItem
{
get { return this.selectedItem; }
set
{
if (value != this.selectedItem)
{
this.selectedItem = value;
this.OnPropertyChanged("SelectedItem");
}
}
}
}
}