forked from galinarudollf/Silberstadt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersonViewModel.cs
71 lines (59 loc) · 2.35 KB
/
PersonViewModel.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
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
namespace Silberstadt;
public class PersonViewModel : INotifyPropertyChanged{
private List<Person> daten;
public event PropertyChangedEventHandler? PropertyChanged;
public List<Person> Daten{
get=>daten;
set{
SetProperty<List<Person>>(ref daten, value);
}
}
private ListView listView;
private Entry entryName;
private Entry entryYears;
public PersonViewModel(ListView _listview, Entry _entryname, Entry _entryyears)
{
listView = _listview;
entryName = _entryname;
entryYears = _entryyears;
Daten=[
new Person(){ Name="Gottfried Silbermann", Data="14.1.1683 Kleinbobritzsch – 4.8.1753 Dresden"},
new Person(){ Name="Abraham Gottlob Werner", Data=" 25.9.1749 Wehrau am Queis – 30.6.1817 Dresden"},
new Person(){ Name="Alexander von Humboldt", Data="1769 Berlin – 1859 Berlin"},
new Person(){ Name="Clemens Winkler", Data="26.12.1838 Freiberg – 8.10.1904 Dresden"}];
UpdateViewList();
MyCommand=new Command(
execute: () =>
{
string name = entryName.Text;
string data = entryYears.Text;
//Daten.Add(new Person { Name = name, Data = data });
//LINQ
Daten = Daten.Append(new Person { Name = name, Data = data }).ToList();
UpdateViewList();
entryName.Text = string.Empty;
entryYears.Text = string.Empty;
});
}
public ICommand MyCommand { private set; get; }
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value)) return false;
storage = value;
NotifyPropertyChanged(propertyName);
return true;
}
private void UpdateViewList(){
// Update the ListView using LINQ
var displayData = from person in Daten
select new { Display = $"{person.Name}, {person.Data}" };
listView.ItemsSource = displayData.ToList();
}
}