回答編集履歴

4

修正

2018/12/15 15:02

投稿

Zuishin
Zuishin

スコア28662

test CHANGED
@@ -112,7 +112,7 @@
112
112
 
113
113
  .ToReactiveCommand(false);
114
114
 
115
- ClearCommand.Subscribe(() => People.Clear());
115
+ ClearCommand.Subscribe(People.Clear);
116
116
 
117
117
  People.Add("who");
118
118
 

3

修正

2018/12/15 15:02

投稿

Zuishin
Zuishin

スコア28662

test CHANGED
@@ -6,9 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- 次のコードでは、起動時に People に要素が一つ入っているのでクリアボタン有効にしています。それを押すと People がクリアされてボタンが無効になります。
9
+ 次のコードでは、起動時に People に要素が一つ入っているのでクリアボタン有効す。それを押すと People がクリアされてボタンが無効になります。
10
-
11
- 最初に要素が 0 の場合は `.ToReactiveCommand(false);` としてクリアボタンの初期状態を無効にしてください。
12
10
 
13
11
 
14
12
 
@@ -106,17 +104,17 @@
106
104
 
107
105
  {
108
106
 
109
- People.Add("who");
110
-
111
107
  ClearCommand = Observable
112
108
 
113
109
  .FromEventPattern<NotifyCollectionChangedEventArgs>(People, nameof(People.CollectionChanged))
114
110
 
115
111
  .Select(_ => People.Count > 0)
116
112
 
117
- .ToReactiveCommand();
113
+ .ToReactiveCommand(false);
118
114
 
119
115
  ClearCommand.Subscribe(() => People.Clear());
116
+
117
+ People.Add("who");
120
118
 
121
119
  }
122
120
 

2

修正

2018/12/15 14:56

投稿

Zuishin
Zuishin

スコア28662

test CHANGED
@@ -6,7 +6,9 @@
6
6
 
7
7
 
8
8
 
9
- 次のコードでは、起動時に People に要素が一つ入っているのでクリアボタン有効になり、それを押すと People がクリアされてボタンが無効になります。
9
+ 次のコードでは、起動時に People に要素が一つ入っているのでクリアボタン有効にしています。それを押すと People がクリアされてボタンが無効になります。
10
+
11
+ 最初に要素が 0 の場合は `.ToReactiveCommand(false);` としてクリアボタンの初期状態を無効にしてください。
10
12
 
11
13
 
12
14
 

1

追記

2018/12/15 14:54

投稿

Zuishin
Zuishin

スコア28662

test CHANGED
@@ -1 +1,125 @@
1
1
  CanExecute を実装したコマンドをバインドしてください。
2
+
3
+
4
+
5
+ #追記
6
+
7
+
8
+
9
+ 次のコードでは、起動時に People に要素が一つ入っているのでクリアボタンが有効になり、それを押すと People がクリアされてボタンが無効になります。
10
+
11
+
12
+
13
+ MainWindow.xaml
14
+
15
+
16
+
17
+ ```XML
18
+
19
+ <Window x:Class="WpfApp1.MainWindow"
20
+
21
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
+
23
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
24
+
25
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
26
+
27
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
28
+
29
+ xmlns:local="clr-namespace:WpfApp1"
30
+
31
+ mc:Ignorable="d"
32
+
33
+ Title="MainWindow" Height="450" Width="800">
34
+
35
+ <Window.DataContext>
36
+
37
+ <local:ViewModel/>
38
+
39
+ </Window.DataContext>
40
+
41
+ <Grid>
42
+
43
+ <Button Command="{Binding ClearCommand}">Clear</Button>
44
+
45
+ </Grid>
46
+
47
+ </Window>
48
+
49
+ ```
50
+
51
+
52
+
53
+ ViewModel.cs
54
+
55
+
56
+
57
+ ```C#
58
+
59
+ using Reactive.Bindings;
60
+
61
+ using System.Collections.ObjectModel;
62
+
63
+ using System.Collections.Specialized;
64
+
65
+ using System.ComponentModel;
66
+
67
+ using System.Linq;
68
+
69
+ using System.Reactive.Linq;
70
+
71
+ using System.Runtime.CompilerServices;
72
+
73
+
74
+
75
+ namespace WpfApp1
76
+
77
+ {
78
+
79
+ public class ViewModel : INotifyPropertyChanged
80
+
81
+ {
82
+
83
+ public event PropertyChangedEventHandler PropertyChanged;
84
+
85
+ protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
86
+
87
+ {
88
+
89
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
90
+
91
+ }
92
+
93
+
94
+
95
+ public ObservableCollection<string> People { get; } = new ObservableCollection<string>();
96
+
97
+
98
+
99
+ public ReactiveCommand ClearCommand { get; private set; }
100
+
101
+
102
+
103
+ public ViewModel()
104
+
105
+ {
106
+
107
+ People.Add("who");
108
+
109
+ ClearCommand = Observable
110
+
111
+ .FromEventPattern<NotifyCollectionChangedEventArgs>(People, nameof(People.CollectionChanged))
112
+
113
+ .Select(_ => People.Count > 0)
114
+
115
+ .ToReactiveCommand();
116
+
117
+ ClearCommand.Subscribe(() => People.Clear());
118
+
119
+ }
120
+
121
+ }
122
+
123
+ }
124
+
125
+ ```