Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OrderedItem.cs
1using System;
2using System.Threading.Tasks;
3using System.Windows.Input;
4
6{
10 public abstract class OrderedItem : Model
11 {
12 private readonly IProperty items;
13
14 private readonly Command moveUp;
15 private readonly Command moveDown;
16
17 public OrderedItem(IProperty Items)
18 : base()
19 {
20 this.items = Items;
21
22 this.moveUp = new Command(this.CanExecuteMoveUp, this.ExecuteMoveUp);
23 this.moveDown = new Command(this.CanExecuteMoveDown, this.ExecuteMoveDown);
24 }
25
29 public ICommand MoveUp => this.moveUp;
30
34 public ICommand MoveDown => this.moveDown;
35
40 public bool CanExecuteMoveUp()
41 {
42 if (this.items.UntypedValue is Array A)
43 return Array.IndexOf(A, this) > 0;
44 else
45 return false;
46 }
47
51 public Task ExecuteMoveUp()
52 {
53 if (this.items.UntypedValue is not Array Items)
54 return Task.CompletedTask;
55
56 Items = (Array)Items.Clone();
57
58 int i = Array.IndexOf(Items, this);
59 if (i <= 0)
60 return Task.CompletedTask;
61
62 object Item1 = Items.GetValue(i - 1);
63 object Item2 = Items.GetValue(i);
64
65 Items.SetValue(Item2, i - 1);
66 Items.SetValue(Item1, i);
67
68 this.items.UntypedValue = Items;
69
70 return Task.CompletedTask;
71 }
72
77 public bool CanExecuteMoveDown()
78 {
79 if (this.items.UntypedValue is Array A)
80 return Array.IndexOf(A, this) < A.Length - 1;
81 else
82 return false;
83 }
84
88 public Task ExecuteMoveDown()
89 {
90 if (this.items.UntypedValue is not Array Items)
91 return Task.CompletedTask;
92
93 Items = (Array)Items.Clone();
94
95 int i = Array.IndexOf(Items, this);
96 if (i < 0 || i >= Items.Length - 1)
97 return Task.CompletedTask;
98
99 object Item1 = Items.GetValue(i + 1);
100 object Item2 = Items.GetValue(i);
101
102 Items.SetValue(Item2, i + 1);
103 Items.SetValue(Item1, i);
104
105 this.items.UntypedValue = Items;
106
107 return Task.CompletedTask;
108 }
109 }
110}