質問するログイン新規登録

回答編集履歴

4

修正

2018/12/15 15:02

投稿

Zuishin
Zuishin

スコア28675

answer CHANGED
@@ -55,7 +55,7 @@
55
55
  .FromEventPattern<NotifyCollectionChangedEventArgs>(People, nameof(People.CollectionChanged))
56
56
  .Select(_ => People.Count > 0)
57
57
  .ToReactiveCommand(false);
58
- ClearCommand.Subscribe(() => People.Clear());
58
+ ClearCommand.Subscribe(People.Clear);
59
59
  People.Add("who");
60
60
  }
61
61
  }

3

修正

2018/12/15 15:02

投稿

Zuishin
Zuishin

スコア28675

answer CHANGED
@@ -2,8 +2,7 @@
2
2
 
3
3
  #追記
4
4
 
5
- 次のコードでは、起動時に People に要素が一つ入っているのでクリアボタン有効にしています。それを押すと People がクリアされてボタンが無効になります。
5
+ 次のコードでは、起動時に People に要素が一つ入っているのでクリアボタン有効す。それを押すと People がクリアされてボタンが無効になります。
6
- 最初に要素が 0 の場合は `.ToReactiveCommand(false);` としてクリアボタンの初期状態を無効にしてください。
7
6
 
8
7
  MainWindow.xaml
9
8
 
@@ -52,12 +51,12 @@
52
51
 
53
52
  public ViewModel()
54
53
  {
55
- People.Add("who");
56
54
  ClearCommand = Observable
57
55
  .FromEventPattern<NotifyCollectionChangedEventArgs>(People, nameof(People.CollectionChanged))
58
56
  .Select(_ => People.Count > 0)
59
- .ToReactiveCommand();
57
+ .ToReactiveCommand(false);
60
58
  ClearCommand.Subscribe(() => People.Clear());
59
+ People.Add("who");
61
60
  }
62
61
  }
63
62
  }

2

修正

2018/12/15 14:56

投稿

Zuishin
Zuishin

スコア28675

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

1

追記

2018/12/15 14:54

投稿

Zuishin
Zuishin

スコア28675

answer CHANGED
@@ -1,1 +1,63 @@
1
- CanExecute を実装したコマンドをバインドしてください。
1
+ CanExecute を実装したコマンドをバインドしてください。
2
+
3
+ #追記
4
+
5
+ 次のコードでは、起動時に People に要素が一つ入っているのでクリアボタンが有効になり、それを押すと People がクリアされてボタンが無効になります。
6
+
7
+ MainWindow.xaml
8
+
9
+ ```XML
10
+ <Window x:Class="WpfApp1.MainWindow"
11
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
12
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
13
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
14
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
15
+ xmlns:local="clr-namespace:WpfApp1"
16
+ mc:Ignorable="d"
17
+ Title="MainWindow" Height="450" Width="800">
18
+ <Window.DataContext>
19
+ <local:ViewModel/>
20
+ </Window.DataContext>
21
+ <Grid>
22
+ <Button Command="{Binding ClearCommand}">Clear</Button>
23
+ </Grid>
24
+ </Window>
25
+ ```
26
+
27
+ ViewModel.cs
28
+
29
+ ```C#
30
+ using Reactive.Bindings;
31
+ using System.Collections.ObjectModel;
32
+ using System.Collections.Specialized;
33
+ using System.ComponentModel;
34
+ using System.Linq;
35
+ using System.Reactive.Linq;
36
+ using System.Runtime.CompilerServices;
37
+
38
+ namespace WpfApp1
39
+ {
40
+ public class ViewModel : INotifyPropertyChanged
41
+ {
42
+ public event PropertyChangedEventHandler PropertyChanged;
43
+ protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
44
+ {
45
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
46
+ }
47
+
48
+ public ObservableCollection<string> People { get; } = new ObservableCollection<string>();
49
+
50
+ public ReactiveCommand ClearCommand { get; private set; }
51
+
52
+ public ViewModel()
53
+ {
54
+ People.Add("who");
55
+ ClearCommand = Observable
56
+ .FromEventPattern<NotifyCollectionChangedEventArgs>(People, nameof(People.CollectionChanged))
57
+ .Select(_ => People.Count > 0)
58
+ .ToReactiveCommand();
59
+ ClearCommand.Subscribe(() => People.Clear());
60
+ }
61
+ }
62
+ }
63
+ ```