-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
52 lines (43 loc) · 1.72 KB
/
MainWindow.xaml.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
using DevExpress.Mvvm;
using DevExpress.Xpf.Grid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
namespace LockOnRowEdit_CodeBehind {
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 partial class MainWindow : Window {
List<DataItem> data;
Timer updateTimer;
bool updatesLocker;
Random random;
public MainWindow() {
InitializeComponent();
random = new Random();
grid.ItemsSource = data = new List<DataItem>(Enumerable.Range(0, 20).Select(i => new DataItem(random, i)));
updateTimer = new Timer(UpdateRows, null, 0, 1);
}
private void OnRowEditStarted(object sender, RowEditStartedEventArgs e) => Volatile.Write(ref updatesLocker, true);
private void OnRowEditFinished(object sender, RowEditFinishedEventArgs e) => 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);
}
}
}
}
}