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

回答編集履歴

2

見直しキャンペーン中

2023/07/21 09:24

投稿

TN8001
TN8001

スコア10169

answer CHANGED
@@ -1,115 +1,115 @@
1
- `Younger()`を実行したときに`Age`が変わるのは、作った人にしかわからないのでどうにかして知らせる必要があります。
2
-
3
- `INotifyPropertyChanged`がそれです。
4
- `ToReactivePropertyAsSynchronized`は`INotifyPropertyChanged`の変更通知を購読し、反映する仕組みですが`Person`側で通知をしていません。
5
- 通知をしてください(Age2)
6
-
7
- `ReactivePropertySlim`を使ってもよいでしょう(Age3)
8
-
9
- `Age { get; set; }`を維持するならViewModel側で変えることになりますが、筋はよくなさそうです(Age1)
10
-
11
- ```xaml
12
- <Window
13
- x:Class="Questions250717.MainWindow"
14
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
15
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
16
- Width="800"
17
- Height="450">
18
- <StackPanel>
19
- <HeaderedContentControl Header="Age1">
20
- <TextBox Text="{Binding Age1.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
21
- </HeaderedContentControl>
22
- <HeaderedContentControl Header="Age2">
23
- <TextBox Text="{Binding Age2.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
24
- </HeaderedContentControl>
25
- <HeaderedContentControl Header="Age3">
26
- <TextBox Text="{Binding Age3.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
27
- </HeaderedContentControl>
28
- <Button Command="{Binding YoungerCommand}">
29
- <TextBlock Text="若返る" />
30
- </Button>
31
- <Button Command="{Binding ElderCommand}">
32
- <TextBlock Text="老いる" />
33
- </Button>
34
- </StackPanel>
35
- </Window>
36
- ```
37
-
38
- ```C#
39
- using System.Windows;
40
- using Prism.Mvvm;
41
- using Reactive.Bindings;
42
- using Reactive.Bindings.Extensions;
43
-
44
- namespace Questions250717
45
- {
46
- public class Person : BindableBase
47
- {
48
- public int Age1 { get; set; }
49
-
50
- public int Age2 { get => _Age2; set => SetProperty(ref _Age2, value); }
51
- private int _Age2;
52
-
53
- public ReactivePropertySlim<int> Age3 { get; }
54
-
55
- public Person()
56
- {
57
- Age1 = 55;
58
- Age2 = 55;
59
- Age3 = new ReactivePropertySlim<int>(55);
60
- }
61
-
62
- public void Younger()
63
- {
64
- //Age1--;
65
- Age2--;
66
- Age3.Value--;
67
- }
68
-
69
- public void Elder()
70
- {
71
- //Age1++;
72
- Age2++;
73
- Age3.Value++;
74
- }
75
- }
76
- public class PersonViewModel : BindableBase
77
- {
78
- public ReactiveProperty<int> Age1 { get; }
79
- public ReactiveProperty<int> Age2 { get; }
80
- public ReactiveProperty<int> Age3 { get; }
81
-
82
- public ReactiveCommand YoungerCommand { get; }
83
- public ReactiveCommand ElderCommand { get; }
84
-
85
- private readonly Person person;
86
-
87
- public PersonViewModel(Person person)
88
- {
89
- this.person = person;
90
- Age1 = ReactiveProperty.FromObject(this.person, x => x.Age1);
91
- Age2 = this.person.ToReactivePropertyAsSynchronized(x => x.Age2);
92
- Age3 = this.person.Age3.ToReactiveProperty();
93
-
94
- YoungerCommand = new ReactiveCommand().WithSubscribe(() =>
95
- {
96
- Age1.Value--;
97
- this.person.Younger();
98
- });
99
- ElderCommand = new ReactiveCommand().WithSubscribe(() =>
100
- {
101
- Age1.Value++;
102
- this.person.Elder();
103
- });
104
- }
105
- }
106
- public partial class MainWindow : Window
107
- {
108
- public MainWindow()
109
- {
110
- InitializeComponent();
111
- DataContext = new PersonViewModel(new Person());
112
- }
113
- }
114
- }
1
+ `Younger()`を実行したときに`Age`が変わるのは、作った人にしかわからないのでどうにかして知らせる必要があります。
2
+
3
+ `INotifyPropertyChanged`がそれです。
4
+ `ToReactivePropertyAsSynchronized`は`INotifyPropertyChanged`の変更通知を購読し、反映する仕組みですが`Person`側で通知をしていません。
5
+ 通知をしてください(Age2)
6
+
7
+ `ReactivePropertySlim`を使ってもよいでしょう(Age3)
8
+
9
+ `Age { get; set; }`を維持するならViewModel側で変えることになりますが、筋はよくなさそうです(Age1)
10
+
11
+ ```xml
12
+ <Window
13
+ x:Class="Questions250717.MainWindow"
14
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
15
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
16
+ Width="800"
17
+ Height="450">
18
+ <StackPanel>
19
+ <HeaderedContentControl Header="Age1">
20
+ <TextBox Text="{Binding Age1.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
21
+ </HeaderedContentControl>
22
+ <HeaderedContentControl Header="Age2">
23
+ <TextBox Text="{Binding Age2.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
24
+ </HeaderedContentControl>
25
+ <HeaderedContentControl Header="Age3">
26
+ <TextBox Text="{Binding Age3.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
27
+ </HeaderedContentControl>
28
+ <Button Command="{Binding YoungerCommand}">
29
+ <TextBlock Text="若返る" />
30
+ </Button>
31
+ <Button Command="{Binding ElderCommand}">
32
+ <TextBlock Text="老いる" />
33
+ </Button>
34
+ </StackPanel>
35
+ </Window>
36
+ ```
37
+
38
+ ```cs
39
+ using System.Windows;
40
+ using Prism.Mvvm;
41
+ using Reactive.Bindings;
42
+ using Reactive.Bindings.Extensions;
43
+
44
+ namespace Questions250717
45
+ {
46
+ public class Person : BindableBase
47
+ {
48
+ public int Age1 { get; set; }
49
+
50
+ public int Age2 { get => _Age2; set => SetProperty(ref _Age2, value); }
51
+ private int _Age2;
52
+
53
+ public ReactivePropertySlim<int> Age3 { get; }
54
+
55
+ public Person()
56
+ {
57
+ Age1 = 55;
58
+ Age2 = 55;
59
+ Age3 = new ReactivePropertySlim<int>(55);
60
+ }
61
+
62
+ public void Younger()
63
+ {
64
+ //Age1--;
65
+ Age2--;
66
+ Age3.Value--;
67
+ }
68
+
69
+ public void Elder()
70
+ {
71
+ //Age1++;
72
+ Age2++;
73
+ Age3.Value++;
74
+ }
75
+ }
76
+ public class PersonViewModel : BindableBase
77
+ {
78
+ public ReactiveProperty<int> Age1 { get; }
79
+ public ReactiveProperty<int> Age2 { get; }
80
+ public ReactiveProperty<int> Age3 { get; }
81
+
82
+ public ReactiveCommand YoungerCommand { get; }
83
+ public ReactiveCommand ElderCommand { get; }
84
+
85
+ private readonly Person person;
86
+
87
+ public PersonViewModel(Person person)
88
+ {
89
+ this.person = person;
90
+ Age1 = ReactiveProperty.FromObject(this.person, x => x.Age1);
91
+ Age2 = this.person.ToReactivePropertyAsSynchronized(x => x.Age2);
92
+ Age3 = this.person.Age3.ToReactiveProperty();
93
+
94
+ YoungerCommand = new ReactiveCommand().WithSubscribe(() =>
95
+ {
96
+ Age1.Value--;
97
+ this.person.Younger();
98
+ });
99
+ ElderCommand = new ReactiveCommand().WithSubscribe(() =>
100
+ {
101
+ Age1.Value++;
102
+ this.person.Elder();
103
+ });
104
+ }
105
+ }
106
+ public partial class MainWindow : Window
107
+ {
108
+ public MainWindow()
109
+ {
110
+ InitializeComponent();
111
+ DataContext = new PersonViewModel(new Person());
112
+ }
113
+ }
114
+ }
115
115
  ```

1

Age1 { get; set; }

2020/04/01 09:36

投稿

TN8001
TN8001

スコア10169

answer CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  `ReactivePropertySlim`を使ってもよいでしょう(Age3)
8
8
 
9
- ModelいじりたくければViewModel側で変えることになりますが、筋はよくなさそうです(Age1)
9
+ `Age { get; set; }`維持するViewModel側で変えることになりますが、筋はよくなさそうです(Age1)
10
10
 
11
11
  ```xaml
12
12
  <Window