回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,187 +1,95 @@
|
|
1
1
|
> あるいは変更通知付きのコレクションを使うとそういった手間が省けます。
|
2
|
-
|
3
2
|
> ObservableStack<T> があるようです(試してはいません)
|
4
3
|
|
4
|
+
試してみました。
|
5
|
+
紹介しておいてなんですが、WPFではあまりテストしていなそうで現時点では自分で用意したほうがよさそうでした(既存アイテムがタプルとして表示されてしまう等)
|
6
|
+
[WPF example is behaving weirdly · Issue #7 · Cysharp/ObservableCollections](https://github.com/Cysharp/ObservableCollections/issues/7)
|
5
7
|
|
6
8
|
|
7
|
-
|
9
|
+
例えばこの辺りを使わせてもらうとこんな感じです。
|
8
|
-
|
9
|
-
紹介しておいてなんですが、WPFではあまりテストしていなそうで現時点では自分で用意したほうがよさそうでした(既存アイテムがタプルとして表示されてしまう等)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
10
|
[c# - Observable Stack and Queue - Stack Overflow](https://stackoverflow.com/questions/3127136/observable-stack-and-queue/56177896#56177896)
|
16
11
|
|
17
|
-
例えばこの辺りを使わせてもらうとこんな感じです。
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
```x
|
12
|
+
```xml
|
22
|
-
|
23
13
|
<Window
|
24
|
-
|
25
14
|
x:Class="Questions361141.MainWindow"
|
26
|
-
|
27
15
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
28
|
-
|
29
16
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
30
|
-
|
31
17
|
Width="800"
|
32
|
-
|
33
18
|
Height="450">
|
34
|
-
|
35
19
|
<DockPanel>
|
36
|
-
|
37
20
|
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
|
38
|
-
|
39
21
|
<Button Click="PushButton_Click" Content="Push" />
|
40
|
-
|
41
22
|
<Button Click="PopButton_Click" Content="Pop" />
|
42
|
-
|
43
23
|
</StackPanel>
|
44
|
-
|
45
24
|
<ListView x:Name="listView" />
|
46
|
-
|
47
25
|
</DockPanel>
|
48
|
-
|
49
26
|
</Window>
|
50
|
-
|
51
27
|
```
|
52
28
|
|
53
|
-
|
54
|
-
|
55
|
-
```
|
29
|
+
```cs
|
56
|
-
|
57
30
|
using System.Collections.Generic;
|
58
|
-
|
59
31
|
using System.Collections.Specialized;
|
60
|
-
|
61
32
|
using System.ComponentModel;
|
62
|
-
|
63
33
|
using System.Windows;
|
64
34
|
|
65
|
-
|
66
|
-
|
67
35
|
namespace Questions361141
|
68
|
-
|
69
36
|
{
|
70
|
-
|
71
37
|
// [c# - Observable Stack and Queue - Stack Overflow](https://stackoverflow.com/questions/3127136/observable-stack-and-queue/56177896#56177896)
|
72
|
-
|
73
38
|
public class ObservableStack<T> : Stack<T>, INotifyCollectionChanged, INotifyPropertyChanged
|
74
|
-
|
75
39
|
{
|
76
|
-
|
77
40
|
public ObservableStack() : base() { }
|
78
|
-
|
79
41
|
public ObservableStack(IEnumerable<T> collection) : base(collection) { }
|
80
|
-
|
81
42
|
public ObservableStack(int capacity) : base(capacity) { }
|
82
43
|
|
83
|
-
|
84
|
-
|
85
44
|
public new virtual T Pop()
|
86
|
-
|
87
45
|
{
|
88
|
-
|
89
46
|
var item = base.Pop();
|
90
|
-
|
91
47
|
OnCollectionChanged(NotifyCollectionChangedAction.Remove, item);
|
92
|
-
|
93
48
|
return item;
|
94
|
-
|
49
|
+
}
|
50
|
+
public new virtual void Push(T item)
|
51
|
+
{
|
52
|
+
base.Push(item);
|
53
|
+
OnCollectionChanged(NotifyCollectionChangedAction.Add, item);
|
54
|
+
}
|
55
|
+
public new virtual void Clear()
|
56
|
+
{
|
57
|
+
base.Clear();
|
58
|
+
OnCollectionChanged(NotifyCollectionChangedAction.Reset, default);
|
95
59
|
}
|
96
60
|
|
61
|
+
public virtual event NotifyCollectionChangedEventHandler CollectionChanged;
|
97
|
-
p
|
62
|
+
protected virtual void OnCollectionChanged(NotifyCollectionChangedAction action, T item)
|
98
|
-
|
99
63
|
{
|
100
|
-
|
101
|
-
base.Push(item);
|
102
|
-
|
103
|
-
|
64
|
+
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action, item, item == null ? -1 : 0));
|
104
|
-
|
65
|
+
OnPropertyChanged(nameof(Count));
|
105
66
|
}
|
106
67
|
|
107
|
-
public new virtual void Clear()
|
108
|
-
|
109
|
-
{
|
110
|
-
|
111
|
-
base.Clear();
|
112
|
-
|
113
|
-
OnCollectionChanged(NotifyCollectionChangedAction.Reset, default);
|
114
|
-
|
115
|
-
}
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
public virtual event NotifyCollectionChangedEventHandler CollectionChanged;
|
120
|
-
|
121
|
-
protected virtual void OnCollectionChanged(NotifyCollectionChangedAction action, T item)
|
122
|
-
|
123
|
-
{
|
124
|
-
|
125
|
-
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action, item, item == null ? -1 : 0));
|
126
|
-
|
127
|
-
OnPropertyChanged(nameof(Count));
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
|
132
|
-
|
133
68
|
public virtual event PropertyChangedEventHandler PropertyChanged;
|
134
|
-
|
135
69
|
protected virtual void OnPropertyChanged(string proertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(proertyName));
|
136
|
-
|
137
70
|
}
|
138
71
|
|
139
72
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
73
|
public partial class MainWindow : Window
|
144
|
-
|
145
74
|
{
|
146
|
-
|
147
75
|
private readonly ObservableStack<int> numbers;
|
148
76
|
|
149
|
-
|
150
|
-
|
151
77
|
public MainWindow()
|
152
|
-
|
153
78
|
{
|
154
|
-
|
155
79
|
InitializeComponent();
|
156
80
|
|
157
|
-
|
158
|
-
|
159
81
|
numbers = new ObservableStack<int>();
|
160
|
-
|
161
82
|
numbers.Push(1);
|
162
|
-
|
163
83
|
numbers.Push(2);
|
164
|
-
|
165
84
|
numbers.Push(3);
|
166
85
|
|
167
|
-
|
168
|
-
|
169
86
|
listView.ItemsSource = numbers;
|
170
|
-
|
171
87
|
}
|
172
88
|
|
173
|
-
|
174
|
-
|
175
89
|
private void PushButton_Click(object sender, RoutedEventArgs e)
|
176
|
-
|
177
90
|
=> numbers.Push(numbers.Count + 1);
|
178
|
-
|
179
91
|
private void PopButton_Click(object sender, RoutedEventArgs e)
|
180
|
-
|
181
92
|
=> numbers.Pop();
|
182
|
-
|
183
93
|
}
|
184
|
-
|
185
94
|
}
|
186
|
-
|
187
95
|
```
|