回答編集履歴

4

見直しキャンペーン中

2023/08/11 09:07

投稿

TN8001
TN8001

スコア9321

test CHANGED
@@ -1,9 +1,8 @@
1
1
  xamlで`"{Binding Path=Name}"`(`"{Binding Name}"`でも同じ意味)と書いた場合、`Name`はプロパティでなければなりません(あとインデクサ)
2
- もしフィールドだった場合、
2
+ もしフィールドだった場合、↓のようなエラーが出力ウィンドウに出ているはずです。
3
3
  ```
4
4
  BindingExpression path error: 'Name' property not found on 'object' ''Plugin' (HashCode=20917673)'.
5
5
  ```
6
- のようなエラーが出力ウィンドウに出ているはずです。
7
6
  [データ バインディングの概要 - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop-wpf/data/data-binding-overview?toc=/dotnet/framework/wpf/data/toc.json&bc=/dotnet/framework/wpf/data/breadcrumb/toc.json)
8
7
  [バインディング ソースの概要 - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/framework/wpf/data/binding-sources-overview)
9
8
 

3

見直しキャンペーン中

2023/07/22 09:26

投稿

TN8001
TN8001

スコア9321

test CHANGED
@@ -1,195 +1,98 @@
1
1
  xamlで`"{Binding Path=Name}"`(`"{Binding Name}"`でも同じ意味)と書いた場合、`Name`はプロパティでなければなりません(あとインデクサ)
2
+ もしフィールドだった場合、
3
+ ```
4
+ BindingExpression path error: 'Name' property not found on 'object' ''Plugin' (HashCode=20917673)'.
5
+ ```
6
+ のようなエラーが出力ウィンドウに出ているはずです。
7
+ [データ バインディングの概要 - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop-wpf/data/data-binding-overview?toc=/dotnet/framework/wpf/data/toc.json&bc=/dotnet/framework/wpf/data/breadcrumb/toc.json)
8
+ [バインディング ソースの概要 - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/framework/wpf/data/binding-sources-overview)
2
9
 
10
+ とりあえず期待通りに動かすコードはこうなります。
11
+ ```xml
12
+ <Window
13
+ x:Class="MainWindow"
14
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
15
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
16
+ Width="525"
17
+ Height="350">
3
- もしフィールドだった場合、
18
+ <Grid>
4
-
19
+ <TreeView x:Name="TreeView" ItemsSource="{Binding PluginEntries}">
20
+ <TreeView.ItemTemplate>
21
+ <HierarchicalDataTemplate ItemsSource="{Binding Path=Functions}">
22
+ <TextBlock Text="{Binding Path=Name}" />
23
+ </HierarchicalDataTemplate>
24
+ </TreeView.ItemTemplate>
25
+ </TreeView>
26
+ </Grid>
27
+ </Window>
5
28
  ```
6
29
 
7
- BindingExpression path error: 'Name' property not found on 'object' ''Plugin' (HashCode=20917673)'.
8
-
9
- ```
10
-
11
- のようなエラーが出力ウィンドウに出ているはずです。
12
-
13
- [データ バインディングの概要 - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop-wpf/data/data-binding-overview?toc=/dotnet/framework/wpf/data/toc.json&bc=/dotnet/framework/wpf/data/breadcrumb/toc.json)
14
-
15
- [バインディング ソースの概要 - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/framework/wpf/data/binding-sources-overview)
16
-
17
-
18
-
19
- とりあえず期待通りに動かすコードはこうなります。
20
-
21
- ```xaml
22
-
23
- <Window
24
-
25
- x:Class="MainWindow"
26
-
27
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
28
-
29
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
30
-
31
- Width="525"
32
-
33
- Height="350">
34
-
35
- <Grid>
36
-
37
- <TreeView x:Name="TreeView" ItemsSource="{Binding PluginEntries}">
38
-
39
- <TreeView.ItemTemplate>
40
-
41
- <HierarchicalDataTemplate ItemsSource="{Binding Path=Functions}">
42
-
43
- <TextBlock Text="{Binding Path=Name}" />
44
-
45
- </HierarchicalDataTemplate>
46
-
47
- </TreeView.ItemTemplate>
48
-
49
- </TreeView>
50
-
51
- </Grid>
52
-
53
- </Window>
54
-
55
- ```
56
-
57
-
58
-
59
- ```VB
60
-
30
+ ```vb
61
31
  Class MainWindow
62
-
63
32
  Sub New()
64
-
65
33
  Me.DataContext = New ViewModel
66
-
67
- End Sub
68
-
69
-
70
-
71
- End Class
72
-
73
-
74
-
75
- Public Class ViewModel
76
-
77
- Private _PluginEntries As List(Of Plugin)
78
-
79
- Public Property PluginEntries As List(Of Plugin)
80
-
81
- Get
82
-
83
- Return _PluginEntries
84
-
85
- End Get
86
-
87
- Set(value As List(Of Plugin))
88
-
89
- _PluginEntries = value
90
-
91
- End Set
92
-
93
- End Property
94
-
95
-
96
-
97
- Sub New()
98
-
99
- Me.PluginEntries = New List(Of Plugin)
100
-
101
- Me.PluginEntries.Add(New Plugin With {
102
-
103
- .Name = "あああ",
104
-
105
- .Functions = New List(Of Plugin) From {
106
-
107
- New Plugin With {.Name = "いいい", .Functions = New List(Of Plugin) From {
108
-
109
- New Plugin With {.Name = "ううう", .Functions = New List(Of Plugin) From {
110
-
111
- New Plugin With {.Name = "えええ"}
112
-
113
- }}
114
-
115
- }}
116
-
117
- }
118
-
119
- })
120
-
121
34
  End Sub
122
35
 
123
36
  End Class
124
37
 
38
+ Public Class ViewModel
39
+ Private _PluginEntries As List(Of Plugin)
40
+ Public Property PluginEntries As List(Of Plugin)
41
+ Get
42
+ Return _PluginEntries
43
+ End Get
44
+ Set(value As List(Of Plugin))
45
+ _PluginEntries = value
46
+ End Set
47
+ End Property
125
48
 
49
+ Sub New()
50
+ Me.PluginEntries = New List(Of Plugin)
51
+ Me.PluginEntries.Add(New Plugin With {
52
+ .Name = "あああ",
53
+ .Functions = New List(Of Plugin) From {
54
+ New Plugin With {.Name = "いいい", .Functions = New List(Of Plugin) From {
55
+ New Plugin With {.Name = "ううう", .Functions = New List(Of Plugin) From {
56
+ New Plugin With {.Name = "えええ"}
57
+ }}
58
+ }}
59
+ }
60
+ })
61
+ End Sub
62
+ End Class
126
63
 
127
64
  Public Class Plugin
128
-
129
65
  Private _Name As String
130
-
131
66
  Private _Functions As List(Of Plugin)
132
67
 
133
-
134
-
135
68
  Public Property Name As String
136
-
137
69
  Get
138
-
139
70
  Return _Name
140
-
141
71
  End Get
142
-
143
72
  Set
144
-
145
73
  _Name = Value
146
-
147
74
  End Set
148
-
149
75
  End Property
150
76
 
151
-
152
-
153
77
  Public Property Functions As List(Of Plugin)
154
-
155
78
  Get
156
-
157
79
  Return _Functions
158
-
159
80
  End Get
160
-
161
81
  Set
162
-
163
82
  _Functions = Value
164
-
165
83
  End Set
166
-
167
84
  End Property
168
-
169
85
  End Class
170
-
171
86
  ```
172
-
173
-
174
87
 
175
88
  ---
176
89
 
177
-
178
-
179
90
  それとも厳密な`MVVM`の話なんですかね??
180
-
181
-
182
91
 
183
92
  例題として`TreeView`はあんまり向きません(ややこしいので)が、厳密に`MVVM`するなら`Class Plugin`に対して`Class PluginViewModel`用意することになります。
184
93
 
185
-
186
-
187
94
  [TreeView が配置された UI で Model-View-ViewModel パターンを使う方法(VB.NET) - 周回遅れのブルース](https://hilapon.hatenadiary.org/entry/20120111/1326273061)
188
-
189
95
  上記リンクでいうと`DirectoryViewModel`が`PluginViewModel`で、`DirectoryInfo`が`Plugin`といった感じですかね?
190
96
 
191
-
192
-
193
97
  今回のような単純な例の場合は、**モデルを直接ビューに公開する**ことはよくあります。
194
-
195
98
  **そのため`Name`をプロパティにする**ということになります。

2

全面的に書き直し

2020/07/22 21:39

投稿

TN8001
TN8001

スコア9321

test CHANGED
@@ -1,30 +1,128 @@
1
- バインドするものはプロパティにしてください(`Name``Functions`)
1
+ xamlで`"{Binding Path=Name}"``"{Binding Name}"`でも同じ意味と書いた場合、`Name`はプロパティでなければなりません(あとインデクサ)
2
+
3
+ もしフィールドだった場合、
4
+
5
+ ```
6
+
7
+ BindingExpression path error: 'Name' property not found on 'object' ''Plugin' (HashCode=20917673)'.
8
+
9
+ ```
10
+
11
+ のようなエラーが出力ウィンドウに出ているはずです。
12
+
13
+ [データ バインディングの概要 - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop-wpf/data/data-binding-overview?toc=/dotnet/framework/wpf/data/toc.json&bc=/dotnet/framework/wpf/data/breadcrumb/toc.json)
14
+
15
+ [バインディング ソースの概要 - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/framework/wpf/data/binding-sources-overview)
2
16
 
3
17
 
4
18
 
5
- とxaml単にこうでいいんじゃいでかね
19
+ とりえず期待通りに動かすコードはこうなります。
6
20
 
7
21
  ```xaml
8
22
 
9
- <TreeView x:Name="TreeView" ItemsSource="{Binding PluginEntries}">
23
+ <Window
10
24
 
11
- <TreeView.ItemTemplate>
25
+ x:Class="MainWindow"
12
26
 
13
- <HierarchicalDataTemplate ItemsSource="{Binding Path=Functions}">
27
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
28
 
15
- <TextBlock Text="{Binding Path=Name}" />
29
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
16
30
 
17
- </HierarchicalDataTemplate>
31
+ Width="525"
18
32
 
19
- </TreeView.ItemTemplate>
33
+ Height="350">
20
34
 
35
+ <Grid>
36
+
37
+ <TreeView x:Name="TreeView" ItemsSource="{Binding PluginEntries}">
38
+
39
+ <TreeView.ItemTemplate>
40
+
41
+ <HierarchicalDataTemplate ItemsSource="{Binding Path=Functions}">
42
+
43
+ <TextBlock Text="{Binding Path=Name}" />
44
+
45
+ </HierarchicalDataTemplate>
46
+
47
+ </TreeView.ItemTemplate>
48
+
21
- </TreeView>
49
+ </TreeView>
50
+
51
+ </Grid>
52
+
53
+ </Window>
22
54
 
23
55
  ```
24
56
 
25
57
 
26
58
 
27
59
  ```VB
60
+
61
+ Class MainWindow
62
+
63
+ Sub New()
64
+
65
+ Me.DataContext = New ViewModel
66
+
67
+ End Sub
68
+
69
+
70
+
71
+ End Class
72
+
73
+
74
+
75
+ Public Class ViewModel
76
+
77
+ Private _PluginEntries As List(Of Plugin)
78
+
79
+ Public Property PluginEntries As List(Of Plugin)
80
+
81
+ Get
82
+
83
+ Return _PluginEntries
84
+
85
+ End Get
86
+
87
+ Set(value As List(Of Plugin))
88
+
89
+ _PluginEntries = value
90
+
91
+ End Set
92
+
93
+ End Property
94
+
95
+
96
+
97
+ Sub New()
98
+
99
+ Me.PluginEntries = New List(Of Plugin)
100
+
101
+ Me.PluginEntries.Add(New Plugin With {
102
+
103
+ .Name = "あああ",
104
+
105
+ .Functions = New List(Of Plugin) From {
106
+
107
+ New Plugin With {.Name = "いいい", .Functions = New List(Of Plugin) From {
108
+
109
+ New Plugin With {.Name = "ううう", .Functions = New List(Of Plugin) From {
110
+
111
+ New Plugin With {.Name = "えええ"}
112
+
113
+ }}
114
+
115
+ }}
116
+
117
+ }
118
+
119
+ })
120
+
121
+ End Sub
122
+
123
+ End Class
124
+
125
+
28
126
 
29
127
  Public Class Plugin
30
128
 
@@ -71,3 +169,27 @@
71
169
  End Class
72
170
 
73
171
  ```
172
+
173
+
174
+
175
+ ---
176
+
177
+
178
+
179
+ それとも厳密な`MVVM`の話なんですかね??
180
+
181
+
182
+
183
+ 例題として`TreeView`はあんまり向きません(ややこしいので)が、厳密に`MVVM`するなら`Class Plugin`に対して`Class PluginViewModel`用意することになります。
184
+
185
+
186
+
187
+ [TreeView が配置された UI で Model-View-ViewModel パターンを使う方法(VB.NET) - 周回遅れのブルース](https://hilapon.hatenadiary.org/entry/20120111/1326273061)
188
+
189
+ 上記リンクでいうと`DirectoryViewModel`が`PluginViewModel`で、`DirectoryInfo`が`Plugin`といった感じですかね?
190
+
191
+
192
+
193
+ 今回のような単純な例の場合は、**モデルを直接ビューに公開する**ことはよくあります。
194
+
195
+ **そのため`Name`をプロパティにする**ということになります。

1

VBコード

2020/07/22 21:39

投稿

TN8001
TN8001

スコア9321

test CHANGED
@@ -21,3 +21,53 @@
21
21
  </TreeView>
22
22
 
23
23
  ```
24
+
25
+
26
+
27
+ ```VB
28
+
29
+ Public Class Plugin
30
+
31
+ Private _Name As String
32
+
33
+ Private _Functions As List(Of Plugin)
34
+
35
+
36
+
37
+ Public Property Name As String
38
+
39
+ Get
40
+
41
+ Return _Name
42
+
43
+ End Get
44
+
45
+ Set
46
+
47
+ _Name = Value
48
+
49
+ End Set
50
+
51
+ End Property
52
+
53
+
54
+
55
+ Public Property Functions As List(Of Plugin)
56
+
57
+ Get
58
+
59
+ Return _Functions
60
+
61
+ End Get
62
+
63
+ Set
64
+
65
+ _Functions = Value
66
+
67
+ End Set
68
+
69
+ End Property
70
+
71
+ End Class
72
+
73
+ ```