-
Notifications
You must be signed in to change notification settings - Fork 6
/
CollectionViewModel.cs
55 lines (48 loc) · 1.64 KB
/
CollectionViewModel.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.CRUD.DataModel;
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;
using System.Collections.Generic;
namespace DevExpress.CRUD.ViewModel {
public abstract class CollectionViewModel<T> : ViewModelBase where T : class {
readonly IDataProvider<T> dataProvider;
protected CollectionViewModel(IDataProvider<T> dataProvider) {
this.dataProvider = dataProvider;
OnRefresh();
}
public IList<T> Entities {
get => GetValue<IList<T>>();
private set => SetValue(value);
}
public string EntitiesErrorMessage {
get => GetValue<string>();
private set => SetValue(value);
}
[Command]
public void OnRefresh(RefreshArgs _) {
OnRefresh();
}
void OnRefresh() {
try {
Entities = dataProvider.Read();
EntitiesErrorMessage = null;
} catch {
Entities = null;
EntitiesErrorMessage = "An error has occurred while establishing a connection to the database. Press F5 to retry the connection.";
}
OnRefreshCore();
}
protected virtual void OnRefreshCore() {
}
[Command]
public void OnUpdateRow(RowValidationArgs args) {
var entity = (T)args.Item;
if(args.IsNewItem)
dataProvider.Create(entity);
else
dataProvider.Update(entity);
}
[Command]
public void OnDelete(RowDeleteArgs args) => dataProvider.Delete((T)args.Row);
}
}