回答編集履歴

1

見直しキャンペーン中

2023/07/29 06:57

投稿

TN8001
TN8001

スコア10022

test CHANGED
@@ -1,187 +1,94 @@
1
1
  `ObserveElementProperty`拡張メソッドがあります。
2
-
3
-
4
2
 
5
3
  [ReactiveProperty v2.1.3をリリースしました - かずきのBlog@hatena](https://blog.okazuki.jp/entry/2015/03/10/213610)
6
4
 
7
-
8
-
9
5
  [MVVM をリアクティブプログラミングで快適に ReactiveProperty オーバービュー 2020 年版 後編 - Qiita](https://qiita.com/okazuki/items/6faac7cb1a7d8a6ad0f2#observeelementproperty)
10
-
11
-
12
6
 
13
7
  おそらく効率的な方法があるのでしょうがよくわからなかったので、何か変更があったら全件探索です^^;
14
8
 
15
-
16
-
17
- ```xaml
9
+ ```xml
18
-
19
10
  <Window
20
-
21
11
  x:Class="Questions362682.MainWindow"
22
-
23
12
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
24
-
25
13
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
26
-
27
14
  Width="800"
28
-
29
15
  Height="450">
30
-
31
16
  <DockPanel>
32
-
33
17
  <DockPanel DockPanel.Dock="Top">
34
-
35
18
  <Button Command="{Binding AddCommand}" Content="Add" />
36
-
37
19
  <TextBlock Text="{Binding IsLargeLength.Value}" />
38
-
39
20
  </DockPanel>
40
-
41
21
  <ItemsControl ItemsSource="{Binding Items}">
42
-
43
22
  <ItemsControl.ItemTemplate>
44
-
45
23
  <DataTemplate>
46
-
47
24
  <DockPanel>
48
-
49
25
  <Button
50
-
51
26
  Command="{Binding DataContext.DellCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}}"
52
-
53
27
  CommandParameter="{Binding}"
54
-
55
28
  Content="Del" />
56
-
57
29
  <TextBox Text="{Binding Id, UpdateSourceTrigger=PropertyChanged}" />
58
-
59
30
  </DockPanel>
60
-
61
31
  </DataTemplate>
62
-
63
32
  </ItemsControl.ItemTemplate>
64
-
65
33
  </ItemsControl>
66
-
67
34
  </DockPanel>
68
-
69
35
  </Window>
70
-
71
36
  ```
72
37
 
73
-
74
-
75
- ```C#
38
+ ```cs
76
-
77
39
  using Prism.Commands;
78
-
79
40
  using Prism.Mvvm;
80
-
81
41
  using Reactive.Bindings;
82
-
83
42
  using Reactive.Bindings.Extensions;
84
-
85
43
  using System.Collections.ObjectModel;
86
-
87
44
  using System.Linq;
88
-
89
45
  using System.Reactive.Linq;
90
-
91
46
  using System.Windows;
92
47
 
93
-
94
-
95
48
  namespace Questions362682
96
-
97
49
  {
98
-
99
50
  public class MyData : BindableBase
100
-
101
51
  {
102
-
103
52
  public string Id { get => _id; set => SetProperty(ref _id, value); }
104
-
105
53
  private string _id;
106
-
107
54
  }
108
55
 
109
-
110
-
111
56
  public class MyDataUserViewModel
112
-
113
57
  {
114
-
115
58
  public ObservableCollection<MyData> Items { get; }
116
-
117
59
  public ReadOnlyReactiveProperty<bool> IsLargeLength { get; }
118
60
 
119
-
120
-
121
61
  public DelegateCommand AddCommand { get; }
122
-
123
62
  public DelegateCommand<MyData> DellCommand { get; }
124
-
125
-
126
63
 
127
64
  private int i;
128
65
 
129
-
130
-
131
66
  public MyDataUserViewModel()
132
-
133
67
  {
134
-
135
68
  Items = new(Enumerable.Range(1, 10).Select(_ => new MyData { Id = $"{i++}", }));
136
69
 
137
-
138
-
139
70
  AddCommand = new(() => Items.Add(new() { Id = $"{i++}", }));
140
-
141
71
  DellCommand = new(x => Items.Remove(x));
142
72
 
143
-
144
-
145
73
  var a = Items.ObserveElementProperty(x => x.Id)
146
-
147
74
  .Select(_ => Items.Any(x => x.Id.Length >= 2));
148
-
149
75
  var b = Items.CollectionChangedAsObservable()
150
-
151
76
  .Select(_ => Items.Any(x => x.Id.Length >= 2));
152
-
153
77
  IsLargeLength = a.Merge(b).ToReadOnlyReactiveProperty();
154
-
155
78
  }
156
-
157
79
  }
158
80
 
159
-
160
-
161
81
  public partial class MainWindow : Window
162
-
163
82
  {
164
-
165
83
  public MainWindow()
166
-
167
84
  {
168
-
169
85
  InitializeComponent();
170
-
171
86
  DataContext = new MyDataUserViewModel();
172
-
173
87
  }
174
-
175
88
  }
176
-
177
89
  }
178
-
179
90
  ```
180
-
181
-
182
91
 
183
92
  ---
184
93
 
185
-
186
-
187
94
  `IFilteredReadOnlyObservableCollection`とかも使えるかな?と思ったんのですが、`Count`を発砲してくれませんでした。。。