-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainViewModel.cs
55 lines (46 loc) · 1.85 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
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Windows.Threading;
namespace LockOnRowEdit_MVVM {
public class DataItem : BindableBase {
public int Id { get => GetValue<int>(); set => SetValue(value); }
public string Name { get => GetValue<string>(); set => SetValue(value);}
public int Value { get => GetValue<int>(); set => SetValue(value); }
public bool ShouldUpdate { get => GetValue<bool>(); set => SetValue(value); }
public DataItem(Random random, int id) {
Id = id;
Name = $"Item {id}";
Value = random.Next(1, 100);
ShouldUpdate = true;
}
}
public class MainViewModel : ViewModelBase {
public ObservableCollection<DataItem> Data { get => GetValue<ObservableCollection<DataItem>>(); set => SetValue(value); }
Random random;
Timer updateTimer;
bool updatesLocker;
public MainViewModel() {
random = new Random();
Data = new ObservableCollection<DataItem>(Enumerable.Range(0, 20).Select(i => new DataItem(random, i)));
updateTimer = new Timer(UpdateRows, null, 0, 1);
}
[Command]
public void LockUpdates(RowEditStartedArgs args) => Volatile.Write(ref updatesLocker, true);
[Command]
public void UnlockUpdates(RowEditFinishedArgs args) => Volatile.Write(ref updatesLocker, false);
void UpdateRows(object parameter) {
if(!Volatile.Read(ref updatesLocker)) {
var row = Data[random.Next(0, Data.Count)];
if(row.ShouldUpdate) {
row.Value = random.Next(1, 100);
}
}
}
}
}