回答編集履歴
3
見直しキャンペーン中
answer
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
動くコードがないと話をすり合わせるのが大変なので^^;
|
2
|
-
今の私の理解を
|
2
|
+
今の私の理解をMVVMを意識せずざっくり実装しました。
|
3
3
|
|
4
4
|
```xml:MainWindow.xaml
|
5
5
|
<Window
|
2
見直しキャンペーン中
answer
CHANGED
@@ -1,207 +1,203 @@
|
|
1
|
-
動くコードがないと話をすり合わせるのが大変なので^^;
|
2
|
-
今の私の理解を`MVVM`を意識せずざっくり実装しました。
|
3
|
-
|
4
|
-
MainWindow.xaml
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml
|
9
|
-
xmlns:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
<
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
<
|
21
|
-
|
22
|
-
|
23
|
-
<RowDefinition />
|
24
|
-
|
25
|
-
</
|
26
|
-
<
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
{
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
}
|
80
|
-
|
81
|
-
public class
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
public
|
86
|
-
|
87
|
-
|
88
|
-
public
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
```
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
xmlns="
|
100
|
-
xmlns:
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
=
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
=
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
```
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
>
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
このへんのニュアンスもいまいちわかりません。
|
204
|
-
モデルとは`CheckedInfoList`のことを指しているんでしょうか?
|
205
|
-
それとも`InfoVM`のために画像をとってくるような処理の部分?
|
206
|
-
|
1
|
+
動くコードがないと話をすり合わせるのが大変なので^^;
|
2
|
+
今の私の理解を`MVVM`を意識せずざっくり実装しました。
|
3
|
+
|
4
|
+
```xml:MainWindow.xaml
|
5
|
+
<Window
|
6
|
+
x:Class="Test.MainWindow"
|
7
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
8
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
9
|
+
xmlns:local="clr-namespace:Test"
|
10
|
+
Height="450"
|
11
|
+
SizeToContent="Width">
|
12
|
+
<Window.DataContext>
|
13
|
+
<local:MainWindowViewModel />
|
14
|
+
</Window.DataContext>
|
15
|
+
<Window.Resources>
|
16
|
+
<DataTemplate x:Key="CheckedInfo" DataType="{x:Type local:Info}">
|
17
|
+
<TextBlock Text="{Binding Path=Name, StringFormat=CheckedInfo: {0}}" />
|
18
|
+
</DataTemplate>
|
19
|
+
</Window.Resources>
|
20
|
+
<Grid>
|
21
|
+
<Grid.RowDefinitions>
|
22
|
+
<RowDefinition />
|
23
|
+
<RowDefinition Height="2*" />
|
24
|
+
</Grid.RowDefinitions>
|
25
|
+
<ListBox ItemTemplate="{StaticResource CheckedInfo}" ItemsSource="{Binding Path=CheckedInfoList, ElementName=testControl}" />
|
26
|
+
<local:TestControl
|
27
|
+
x:Name="testControl"
|
28
|
+
Grid.Row="1"
|
29
|
+
InfoList="{Binding Path=InfoList}" />
|
30
|
+
</Grid>
|
31
|
+
</Window>
|
32
|
+
```
|
33
|
+
|
34
|
+
```cs:MainWindowViewModel.cs
|
35
|
+
using System.Collections.Generic;
|
36
|
+
|
37
|
+
namespace Test
|
38
|
+
{
|
39
|
+
public class MainWindowViewModel //: Observable
|
40
|
+
{
|
41
|
+
public InfoList InfoList { get; }
|
42
|
+
|
43
|
+
//private InfoList _CheckedInfoList;
|
44
|
+
//public InfoList CheckedInfoList { get => _CheckedInfoList; set => Set(ref _CheckedInfoList, value); }
|
45
|
+
|
46
|
+
public MainWindowViewModel()
|
47
|
+
{
|
48
|
+
InfoList = new InfoList
|
49
|
+
{
|
50
|
+
new Info {
|
51
|
+
Name = "1",
|
52
|
+
Child = new InfoList {
|
53
|
+
new Info {
|
54
|
+
Name = "1-1",
|
55
|
+
Child = new InfoList {
|
56
|
+
new Info { Name = "1-1-1", Check = true, },
|
57
|
+
new Info { Name = "1-1-2", },
|
58
|
+
},
|
59
|
+
},
|
60
|
+
},
|
61
|
+
},
|
62
|
+
new Info {
|
63
|
+
Name = "2",
|
64
|
+
Child = new InfoList {
|
65
|
+
new Info { Name = "2-1", Check = true, },
|
66
|
+
},
|
67
|
+
},
|
68
|
+
new Info { Name = "3", Check = true, },
|
69
|
+
new Info {
|
70
|
+
Name = "4",
|
71
|
+
Child = new InfoList {
|
72
|
+
new Info { Name = "4-1", },
|
73
|
+
},
|
74
|
+
},
|
75
|
+
};
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
public class InfoList : List<Info> { }
|
80
|
+
|
81
|
+
public class Info
|
82
|
+
{
|
83
|
+
public string Name { get; set; }
|
84
|
+
//public string Icon { get; set; }
|
85
|
+
public bool Expand { get; set; } = true;
|
86
|
+
public bool Check { get; set; }
|
87
|
+
|
88
|
+
public InfoList Child { get; set; } // Info Child?
|
89
|
+
}
|
90
|
+
}
|
91
|
+
```
|
92
|
+
|
93
|
+
```xml:TestControl.xaml
|
94
|
+
<UserControl
|
95
|
+
x:Class="Test.TestControl"
|
96
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
97
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
98
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
99
|
+
xmlns:local="clr-namespace:Test"
|
100
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
101
|
+
x:Name="userControl"
|
102
|
+
Width="300"
|
103
|
+
d:DesignHeight="400"
|
104
|
+
mc:Ignorable="d">
|
105
|
+
<Grid Name="baseContainer">
|
106
|
+
<TreeView
|
107
|
+
CheckBox.Checked="TreeView_CheckChanged"
|
108
|
+
CheckBox.Unchecked="TreeView_CheckChanged"
|
109
|
+
ItemsSource="{Binding InfoList, ElementName=userControl, Mode=OneWay}">
|
110
|
+
<TreeView.Resources>
|
111
|
+
<Style TargetType="TreeViewItem">
|
112
|
+
<Setter Property="IsExpanded" Value="{Binding Path=Expand, Mode=TwoWay}" />
|
113
|
+
</Style>
|
114
|
+
</TreeView.Resources>
|
115
|
+
|
116
|
+
<TreeView.ItemTemplate>
|
117
|
+
<HierarchicalDataTemplate DataType="local:InfoList" ItemsSource="{Binding Path=Child}">
|
118
|
+
<StackPanel Orientation="Horizontal">
|
119
|
+
<CheckBox IsChecked="{Binding Path=Check, Mode=TwoWay}" />
|
120
|
+
<!--<Image Source="{Binding Path=Icon}" />-->
|
121
|
+
<TextBlock Text="{Binding Path=Name}" />
|
122
|
+
</StackPanel>
|
123
|
+
</HierarchicalDataTemplate>
|
124
|
+
</TreeView.ItemTemplate>
|
125
|
+
</TreeView>
|
126
|
+
</Grid>
|
127
|
+
</UserControl>
|
128
|
+
```
|
129
|
+
|
130
|
+
```cs:TestControl.xaml.cs
|
131
|
+
using System.Windows;
|
132
|
+
using System.Windows.Controls;
|
133
|
+
|
134
|
+
namespace Test
|
135
|
+
{
|
136
|
+
public partial class TestControl : UserControl
|
137
|
+
{
|
138
|
+
public static readonly DependencyProperty InfoListProperty
|
139
|
+
= DependencyProperty.Register(nameof(InfoList), typeof(InfoList),
|
140
|
+
typeof(TestControl), new PropertyMetadata(null));
|
141
|
+
public InfoList InfoList
|
142
|
+
{
|
143
|
+
get => (InfoList)GetValue(InfoListProperty);
|
144
|
+
set => SetValue(InfoListProperty, value);
|
145
|
+
}
|
146
|
+
|
147
|
+
public static readonly DependencyProperty CheckedInfoListProperty
|
148
|
+
= DependencyProperty.Register(nameof(CheckedInfoList), typeof(InfoList),
|
149
|
+
typeof(TestControl), new PropertyMetadata(null));
|
150
|
+
public InfoList CheckedInfoList
|
151
|
+
{
|
152
|
+
get => (InfoList)GetValue(CheckedInfoListProperty);
|
153
|
+
set => SetValue(CheckedInfoListProperty, value);
|
154
|
+
}
|
155
|
+
|
156
|
+
|
157
|
+
public TestControl()
|
158
|
+
{
|
159
|
+
InitializeComponent();
|
160
|
+
}
|
161
|
+
|
162
|
+
private void TreeView_CheckChanged(object sender, RoutedEventArgs e)
|
163
|
+
{
|
164
|
+
var l = new InfoList();
|
165
|
+
foreach(var c in InfoList)
|
166
|
+
{
|
167
|
+
if(Dfs(c)) l.Add(c);
|
168
|
+
}
|
169
|
+
CheckedInfoList = l;
|
170
|
+
|
171
|
+
bool Dfs(Info info)
|
172
|
+
{
|
173
|
+
if(info.Check) return true;
|
174
|
+
if(info.Child == null) return false;
|
175
|
+
foreach(var c in info.Child)
|
176
|
+
{
|
177
|
+
if(Dfs(c)) return true;
|
178
|
+
}
|
179
|
+
|
180
|
+
return false;
|
181
|
+
}
|
182
|
+
}
|
183
|
+
}
|
184
|
+
}
|
185
|
+
```
|
186
|
+

|
187
|
+
|
188
|
+
動作はこれでいいのでしょうか?
|
189
|
+
これですと`TestControl`にVMがありませんが、実際は`MainWindowViewModel`から渡された`InfoList`を`TestControl`で`InfoListVM`等に加工(詰め替え)しているということでしょうか?(何のために??)
|
190
|
+
それとも`Info`が`Check`・`Child`だけで(`Name`もか?)、`InfoVM`に`Icon`や`Expand`を生やしてるってことなのかな?
|
191
|
+
|
192
|
+
|
193
|
+
> データの実体はモデル部より供給
|
194
|
+
|
195
|
+
> check状態(開始時) App --> UserControl v --> vm --> model
|
196
|
+
> check状態(終了時) App <-- UserControl v <-- vm <-- model
|
197
|
+
> ツリー構造の実体 UserControl v <-- vm <-- model
|
198
|
+
|
199
|
+
このへんのニュアンスもいまいちわかりません。
|
200
|
+
モデルとは`CheckedInfoList`のことを指しているんでしょうか?
|
201
|
+
それとも`InfoVM`のために画像をとってくるような処理の部分?
|
202
|
+
|
207
203
|
もしもあまりに乖離しているようですと、VM・Mも出せる範囲で出していただかないと理解できそうにありません^^;
|
1
画像
answer
CHANGED
@@ -187,6 +187,7 @@
|
|
187
187
|
}
|
188
188
|
}
|
189
189
|
```
|
190
|
+

|
190
191
|
|
191
192
|
動作はこれでいいのでしょうか?
|
192
193
|
これですと`TestControl`にVMがありませんが、実際は`MainWindowViewModel`から渡された`InfoList`を`TestControl`で`InfoListVM`等に加工(詰め替え)しているということでしょうか?(何のために??)
|