回答編集履歴

2

見直しキャンペーン中

2023/08/15 09:34

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -6,8 +6,7 @@
6
6
 
7
7
  [MVVM をリアクティブプログラミングで快適に ReactiveProperty オーバービュー 2020 年版 中編 - Qiita](https://qiita.com/okazuki/items/dae37c42776727e6c8a5#reactivecommand)
8
8
 
9
- `ReactiveCommand`(ReactivePropertyの`ICommand`実装)でもほぼ同じですが、現状Reactive要素がまったくないので
9
+ `ReactiveCommand`(ReactivePropertyの`ICommand`実装)でもほぼ同じですが、現状Reactive要素がまったくないので`DelegateCommand`(Prismの`ICommand`実装)でやりました。
10
- `DelegateCommand`(Prismの`ICommand`実装)でやりました。
11
10
 
12
11
  しかしこれではViewModelが`DataGrid`に依存しますしテストもしにくく、あまりメリットがない気がします。
13
12
  こうするぐらいならまだコードビハインドのほうがましだったと(個人的には)感じます。

1

見直しキャンペーン中

2023/07/29 13:15

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -1,365 +1,183 @@
1
1
  > Clickイベントの動作をViewModelで実現したいです。
2
-
3
-
4
2
 
5
3
  「ボタンを押したらViewModelの何かの処理をする」ような場合は、コマンド(`ICommand`)を使います。
6
4
 
7
-
8
-
9
5
  [Commanding | Prism](https://prismlibrary.com/docs/commanding.html)
10
-
11
-
12
6
 
13
7
  [MVVM をリアクティブプログラミングで快適に ReactiveProperty オーバービュー 2020 年版 中編 - Qiita](https://qiita.com/okazuki/items/dae37c42776727e6c8a5#reactivecommand)
14
8
 
15
-
16
-
17
9
  `ReactiveCommand`(ReactivePropertyの`ICommand`実装)でもほぼ同じですが、現状Reactive要素がまったくないので
18
-
19
10
  `DelegateCommand`(Prismの`ICommand`実装)でやりました。
20
11
 
21
-
22
-
23
12
  しかしこれではViewModelが`DataGrid`に依存しますしテストもしにくく、あまりメリットがない気がします。
24
-
25
13
  こうするぐらいならまだコードビハインドのほうがましだったと(個人的には)感じます。
26
-
27
-
28
-
29
14
 
30
15
 
31
16
  > ScrollIntoViewの実装方法が知りたいです。
32
17
 
33
-
34
-
35
18
  実装できていると思うんですが、↑の話を踏まえてMVVMでのよりよい実装って意味ですか?
36
19
 
37
-
38
-
39
20
  MVVM絶対主義者(コードビハインドを書いたら負け勢)は、`Behavior`とか`TriggerAction`とかでやるんですかね?(わたしは絶対主義者ではありません)
40
-
41
21
  試しにやってみましたが、xamlが長くなっただけな気もします^^;
42
22
 
43
-
44
-
45
- ```xaml
23
+ ```xml
46
-
47
24
  <Window
48
-
49
25
  x:Class="Questions375184.Views.MainWindow"
50
-
51
26
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
52
-
53
27
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
54
-
55
28
  xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
56
-
57
29
  xmlns:prism="http://prismlibrary.com/"
58
-
59
30
  xmlns:v="clr-namespace:Questions375184.Views"
60
-
61
31
  Title="{Binding Title}"
62
-
63
32
  Width="800"
64
-
65
33
  Height="600"
66
-
67
34
  prism:ViewModelLocator.AutoWireViewModel="True">
68
-
69
35
  <DockPanel>
70
-
71
36
  <!--<ContentControl prism:RegionManager.RegionName="ContentRegion" />-->
72
-
73
37
  <StackPanel DockPanel.Dock="Bottom" Orientation="Vertical">
74
-
75
38
  <Button Click="BtnTopLine_Click" Content="BtnTopLine_Click" />
76
-
77
39
  <Button Click="BtnLastLine_Click" Content="BtnLastLine_Click" />
78
-
79
40
 
80
-
81
41
  <Separator />
82
-
83
42
 
84
-
85
43
  <Button
86
-
87
44
  Command="{Binding ScrollTopCommand}"
88
-
89
45
  CommandParameter="{Binding ElementName=DataGrid1}"
90
-
91
46
  Content="ScrollTopCommand" />
92
-
93
47
  <Button
94
-
95
48
  Command="{Binding ScrollEndCommand}"
96
-
97
49
  CommandParameter="{Binding ElementName=DataGrid1}"
98
-
99
50
  Content="ScrollEndCommand" />
100
-
101
51
 
102
-
103
52
  <Separator />
104
-
105
53
 
106
-
107
54
  <Button Content="ScrollTopAction">
108
-
109
55
  <i:Interaction.Triggers>
110
-
111
56
  <i:EventTrigger EventName="Click">
112
-
113
57
  <v:ScrollTopAction TargetObject="{Binding ElementName=DataGrid1}" />
114
-
115
58
  </i:EventTrigger>
116
-
117
59
  </i:Interaction.Triggers>
118
-
119
60
  </Button>
120
-
121
61
  <Button Content="ScrollEndAction">
122
-
123
62
  <i:Interaction.Triggers>
124
-
125
63
  <i:EventTrigger EventName="Click">
126
-
127
64
  <v:ScrollEndAction TargetObject="{Binding ElementName=DataGrid1}" />
128
-
129
65
  </i:EventTrigger>
130
-
131
66
  </i:Interaction.Triggers>
132
-
133
67
  </Button>
134
-
135
68
  </StackPanel>
136
-
137
69
 
138
-
139
70
  <DataGrid
140
-
141
71
  Name="DataGrid1"
142
-
143
72
  IsReadOnly="True"
144
-
145
73
  ItemsSource="{Binding DataSource1}" />
146
-
147
74
  </DockPanel>
148
-
149
75
  </Window>
150
-
151
76
  ```
152
77
 
153
-
154
-
155
- ```C#
78
+ ```cs
156
-
157
79
  using System.Windows;
158
-
159
80
  using Microsoft.Xaml.Behaviors;
160
-
161
81
  using System.Windows.Controls;
162
82
 
163
-
164
-
165
83
  namespace Questions375184.Views
166
-
167
84
  {
168
-
169
85
  public partial class MainWindow : Window
170
-
171
86
  {
172
-
173
87
  public MainWindow() => InitializeComponent();
174
88
 
175
-
176
-
177
89
  private void BtnTopLine_Click(object sender, RoutedEventArgs e)
178
-
179
90
  {
180
-
181
91
  DataGrid1.Focus();
182
-
183
92
  DataGrid1.ScrollIntoView(DataGrid1.Items[0]);
184
-
185
93
  }
186
94
 
187
-
188
-
189
95
  private void BtnLastLine_Click(object sender, RoutedEventArgs e)
190
-
191
96
  {
192
-
193
97
  DataGrid1.Focus();
194
-
195
98
  DataGrid1.ScrollIntoView(DataGrid1.Items[^1]);
196
-
197
99
  }
198
-
199
100
  }
200
101
 
201
-
202
-
203
102
  public class ScrollTopAction : TargetedTriggerAction<DataGrid>
204
-
205
103
  {
206
-
207
104
  protected override void Invoke(object parameter)
208
-
209
105
  {
210
-
211
106
  Target.Focus();
212
-
213
107
  Target.ScrollIntoView(Target.Items[0]);
214
-
215
108
  }
216
-
217
109
  }
218
110
 
219
-
220
-
221
111
  public class ScrollEndAction : TargetedTriggerAction<DataGrid>
222
-
223
112
  {
224
-
225
113
  protected override void Invoke(object parameter)
226
-
227
114
  {
228
-
229
115
  Target.Focus();
230
-
231
116
  Target.ScrollIntoView(Target.Items[^1]);
232
-
233
117
  }
234
-
235
118
  }
236
-
237
119
  }
238
-
239
120
  ```
240
121
 
241
-
242
-
243
- ```C#
122
+ ```cs
244
-
245
123
  using System.Data;
246
-
247
124
  using System.Windows.Controls;
248
-
249
125
  using Prism.Commands;
250
-
251
126
  using Prism.Mvvm;
252
127
 
253
-
254
-
255
128
  namespace Questions375184.ViewModels
256
-
257
129
  {
258
-
259
130
  public class MainWindowViewModel : BindableBase
260
-
261
131
  {
262
-
263
132
  public string Title { get => _title; set => SetProperty(ref _title, value); }
264
-
265
133
  private string _title = "Prism Application";
266
134
 
267
-
268
-
269
135
  public DataTable DataSource1 { get => _dataSource1; set => SetProperty(ref _dataSource1, value); }
270
-
271
136
  private DataTable _dataSource1;
272
137
 
273
-
274
-
275
138
  public DelegateCommand<DataGrid> ScrollTopCommand { get; }
276
-
277
139
  public DelegateCommand<DataGrid> ScrollEndCommand { get; }
278
140
 
279
141
 
280
-
281
-
282
-
283
142
  public MainWindowViewModel()
284
-
285
143
  {
286
-
287
144
  var columnCount = 20;
288
-
289
145
  var rowCount = 20;
290
-
291
146
  var dt = new DataTable();
292
147
 
293
-
294
-
295
148
  for (var i = 0; i < columnCount; i++)
296
-
297
149
  {
298
-
299
150
  dt.Columns.Add($"Column{i}");
300
-
301
151
  }
302
152
 
303
-
304
-
305
153
  for (var i = 0; i < rowCount; i++)
306
-
307
154
  {
308
-
309
155
  var row = dt.NewRow();
310
-
311
156
  for (var k = 0; k < columnCount; k++)
312
-
313
157
  {
314
-
315
158
  row[k] = $"{i}-{k}";
316
-
317
159
  }
318
-
319
160
  dt.Rows.Add(row);
320
-
321
161
  }
322
-
323
-
324
162
 
325
163
  DataSource1 = dt;
326
164
 
327
165
 
328
-
329
-
330
-
331
166
  ScrollTopCommand = new DelegateCommand<DataGrid>(ScrollToTop);
332
-
333
167
  ScrollEndCommand = new DelegateCommand<DataGrid>(ScrollToEnd);
334
-
335
168
  }
336
169
 
337
-
338
-
339
170
  private void ScrollToTop(DataGrid dataGrid)
340
-
341
171
  {
342
-
343
172
  dataGrid.Focus();
344
-
345
173
  dataGrid.ScrollIntoView(dataGrid.Items[0]);
346
-
347
174
  }
348
175
 
349
-
350
-
351
176
  private void ScrollToEnd(DataGrid dataGrid)
352
-
353
177
  {
354
-
355
178
  dataGrid.Focus();
356
-
357
179
  dataGrid.ScrollIntoView(dataGrid.Items[^1]);
358
-
359
180
  }
360
-
361
181
  }
362
-
363
182
  }
364
-
365
183
  ```