回答編集履歴

2

見直しキャンペーン中

2023/07/21 12:51

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,4 +1,4 @@
1
- Stack Overflowの例のような分け方をしたことがないのでどういう意図なのがよくわかっていませんが、この例では単に`ViewModel`に`bool`を持っていればいいだけに見えます(`DependencyProperty`を作るのが正しい場合もあるでしょうが)
1
+ Stack Overflowの例のような分け方をしたことがないのでどういう意図かわかませんが、この例では単に`ViewModel`に`bool`を持っていればいいだけに見えます(`DependencyProperty`を作るのが正しい場合もあるでしょうが)
2
2
 
3
3
  `View`に状態があるのならば`ViewModel`にも同じ状態を持たせ、それぞれ`Binding`すれば(回りくどく感じるかもしれませんが)疎結合を保てます。
4
4
 

1

見直しキャンペーン中

2023/07/21 12:49

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,151 +1,74 @@
1
- Stack Overflowの例のような分け方をしたことがないので、どういう意図なのかがよくわかっていませんが、
2
-
3
- 回答のように`DependencyProperty`作るの正し場合もあるしょうが、この例では単に`ViewModel`に`bool`を持っていればいいだけに見えます
1
+ Stack Overflow例のような分け方したこという意図なのかよくわかっていませんが、この例では単に`ViewModel`に`bool`を持っていればいいだけに見えます(`DependencyProperty`を作るのが正しい場合もあるでしょうが)
4
-
5
-
6
2
 
7
3
  `View`に状態があるのならば`ViewModel`にも同じ状態を持たせ、それぞれ`Binding`すれば(回りくどく感じるかもしれませんが)疎結合を保てます。
8
4
 
9
-
10
-
11
- MainWindow.xaml
5
+ ```xml:MainWindow.xaml
12
-
13
- ```xaml
14
-
15
6
  <Window
16
-
17
7
  x:Class="Questions257422.MainWindow"
18
-
19
8
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
20
-
21
9
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
22
-
23
10
  xmlns:local="clr-namespace:Questions257422"
24
-
25
11
  Width="800"
26
-
27
12
  Height="450">
28
-
29
13
  <Window.DataContext>
30
-
31
14
  <local:ViewModel />
32
-
33
15
  </Window.DataContext>
34
-
35
16
  <DockPanel>
36
-
37
17
  <local:Menu DockPanel.Dock="Top" />
38
-
39
18
  <local:Pane />
40
-
41
19
  </DockPanel>
42
-
43
20
  </Window>
44
-
45
21
  ```
46
22
 
47
- Menu.xaml
23
+ ```xml:Menu.xaml
48
-
49
- ```xaml
50
-
51
24
  <UserControl
52
-
53
25
  x:Class="Questions257422.Menu"
54
-
55
26
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
56
-
57
27
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
58
-
59
28
  <Menu>
60
-
61
29
  <MenuItem Header="メニュー1">
62
-
63
30
  <MenuItem
64
-
65
31
  Header="PaneEnabled"
66
-
67
32
  IsCheckable="True"
68
-
69
33
  IsChecked="{Binding PaneEnabled.Value}" />
70
-
71
34
  <MenuItem
72
-
73
35
  Header="LabelVisible"
74
-
75
36
  IsCheckable="True"
76
-
77
37
  IsChecked="{Binding LabelVisible.Value}" />
78
-
79
38
  </MenuItem>
80
-
81
39
  </Menu>
82
-
83
40
  </UserControl>
84
-
85
41
  ```
86
42
 
87
- Pane.xaml
43
+ ```xml:Pane.xaml
88
-
89
- ```xaml
90
-
91
44
  <UserControl
92
-
93
45
  x:Class="Questions257422.Pane"
94
-
95
46
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
96
-
97
47
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
98
-
99
48
  <UserControl.Resources>
100
-
101
49
  <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
102
-
103
50
  </UserControl.Resources>
104
-
105
51
  <StackPanel IsEnabled="{Binding PaneEnabled.Value}">
106
-
107
52
  <Label Visibility="{Binding LabelVisible.Value, Converter={StaticResource BooleanToVisibilityConverter}}">ラベル</Label>
108
-
109
53
  <Button Content="Pane" />
110
-
111
54
  </StackPanel>
112
-
113
55
  </UserControl>
114
-
115
56
  ```
116
57
 
117
- ViewModel.cs
58
+ ```cs:ViewModel.cs
118
-
119
- ```C#
120
-
121
59
  using System.ComponentModel;
122
-
123
60
  using Reactive.Bindings;
124
61
 
125
-
126
-
127
62
  namespace Questions257422
128
-
129
63
  {
130
-
131
64
  class ViewModel : INotifyPropertyChanged
132
-
133
65
  {
134
-
135
66
  public event PropertyChangedEventHandler PropertyChanged;
136
67
 
137
-
138
-
139
68
  // ReactivePropertyを使用 ほかの手段でもいいです
140
-
141
69
  public ReactiveProperty<bool> PaneEnabled { get; } = new ReactiveProperty<bool>();
142
-
143
70
  public ReactiveProperty<bool> LabelVisible { get; } = new ReactiveProperty<bool>(true);
144
-
145
71
  }
146
-
147
72
  }
148
-
149
73
  ```
150
-
151
74
  各xaml.csは初期状態