回答編集履歴

2

見直しキャンペーン中

2023/08/12 10:03

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,6 +1,6 @@
1
1
  `ListBox`の`SelectedIndex`等は複数選択モードだと使い物にならないのですが、選択されているかどうかの判定には使えます。
2
2
 
3
- `SelectedValue`・`SelectedItem`を使ってもいいです(ReactiveProperty<Person>になるだけです)
3
+ `SelectedValue`・`SelectedItem`を使ってもいいです(`ReactiveProperty<Person>`になるだけです)
4
4
 
5
5
  ```xml
6
6
  <ListBox SelectedIndex="{Binding SelectedIndex.Value}">

1

見直しキャンペーン中

2023/07/23 06:37

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,87 +1,44 @@
1
1
  `ListBox`の`SelectedIndex`等は複数選択モードだと使い物にならないのですが、選択されているかどうかの判定には使えます。
2
-
3
-
4
2
 
5
3
  `SelectedValue`・`SelectedItem`を使ってもいいです(ReactiveProperty<Person>になるだけです)
6
4
 
7
-
8
-
9
- ```xaml
5
+ ```xml
10
-
11
6
  <ListBox SelectedIndex="{Binding SelectedIndex.Value}">
12
-
13
7
  ```
14
-
15
- ```C#
8
+ ```cs
16
-
17
9
  public class MainWindowViewModel : BindableBase
18
-
19
10
  {
20
-
21
11
  public ObservableCollection<Person> MyList { get; }
22
-
23
12
  public ReactiveCommand Cmd_DeleteList { get; }
24
-
25
13
  public ReactiveProperty<int> SelectedIndex { get; } = new ReactiveProperty<int>();
26
14
 
27
-
28
-
29
15
  public MainWindowViewModel()
30
-
31
16
  {
32
-
33
17
  MyList = new ObservableCollection<Person>
34
-
35
18
  {
36
-
37
19
  new Person { Name = "AAA" },
38
-
39
20
  new Person { Name = "BBB" },
40
-
41
21
  new Person { Name = "CCC" },
42
-
43
22
  };
44
23
 
45
-
46
-
47
24
  Cmd_DeleteList =
48
-
49
25
  // SelectedIndexが-1でなければtrue
50
-
51
26
  SelectedIndex.Select(x => x != -1)
52
-
53
27
  // ↑の時に実行可能なコマンド作成
54
-
55
28
  .ToReactiveCommand()
56
-
57
29
  // ついでに購読
58
-
59
30
  .WithSubscribe(() =>
60
-
61
31
  {
62
-
63
32
  // 削除候補
64
-
65
33
  var delPersons = MyList.Where(x => x.IsSelected.Value).ToArray();
66
-
67
34
  foreach(var person in delPersons)
68
-
69
35
  {
70
-
71
36
  MyList.Remove(person);
72
-
73
37
  }
74
38
 
75
-
76
-
77
39
  // 選択はすべて削除されたはずなので選択解除
78
-
79
40
  SelectedIndex.Value = -1;
80
-
81
41
  });
82
-
83
42
  }
84
-
85
43
  }
86
-
87
44
  ```