回答編集履歴
4
見直しキャンペーン中
answer
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
見直しキャンペーン中
answer
CHANGED
@@ -1,98 +1,98 @@
|
|
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)
|
9
|
-
|
10
|
-
とりあえず期待通りに動かすコードはこうなります。
|
11
|
-
```
|
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">
|
18
|
-
<Grid>
|
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>
|
28
|
-
```
|
29
|
-
|
30
|
-
```
|
31
|
-
Class MainWindow
|
32
|
-
Sub New()
|
33
|
-
Me.DataContext = New ViewModel
|
34
|
-
End Sub
|
35
|
-
|
36
|
-
End Class
|
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
|
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
|
63
|
-
|
64
|
-
Public Class Plugin
|
65
|
-
Private _Name As String
|
66
|
-
Private _Functions As List(Of Plugin)
|
67
|
-
|
68
|
-
Public Property Name As String
|
69
|
-
Get
|
70
|
-
Return _Name
|
71
|
-
End Get
|
72
|
-
Set
|
73
|
-
_Name = Value
|
74
|
-
End Set
|
75
|
-
End Property
|
76
|
-
|
77
|
-
Public Property Functions As List(Of Plugin)
|
78
|
-
Get
|
79
|
-
Return _Functions
|
80
|
-
End Get
|
81
|
-
Set
|
82
|
-
_Functions = Value
|
83
|
-
End Set
|
84
|
-
End Property
|
85
|
-
End Class
|
86
|
-
```
|
87
|
-
|
88
|
-
---
|
89
|
-
|
90
|
-
それとも厳密な`MVVM`の話なんですかね??
|
91
|
-
|
92
|
-
例題として`TreeView`はあんまり向きません(ややこしいので)が、厳密に`MVVM`するなら`Class Plugin`に対して`Class PluginViewModel`用意することになります。
|
93
|
-
|
94
|
-
[TreeView が配置された UI で Model-View-ViewModel パターンを使う方法(VB.NET) - 周回遅れのブルース](https://hilapon.hatenadiary.org/entry/20120111/1326273061)
|
95
|
-
上記リンクでいうと`DirectoryViewModel`が`PluginViewModel`で、`DirectoryInfo`が`Plugin`といった感じですかね?
|
96
|
-
|
97
|
-
今回のような単純な例の場合は、**モデルを直接ビューに公開する**ことはよくあります。
|
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)
|
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">
|
18
|
+
<Grid>
|
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>
|
28
|
+
```
|
29
|
+
|
30
|
+
```vb
|
31
|
+
Class MainWindow
|
32
|
+
Sub New()
|
33
|
+
Me.DataContext = New ViewModel
|
34
|
+
End Sub
|
35
|
+
|
36
|
+
End Class
|
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
|
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
|
63
|
+
|
64
|
+
Public Class Plugin
|
65
|
+
Private _Name As String
|
66
|
+
Private _Functions As List(Of Plugin)
|
67
|
+
|
68
|
+
Public Property Name As String
|
69
|
+
Get
|
70
|
+
Return _Name
|
71
|
+
End Get
|
72
|
+
Set
|
73
|
+
_Name = Value
|
74
|
+
End Set
|
75
|
+
End Property
|
76
|
+
|
77
|
+
Public Property Functions As List(Of Plugin)
|
78
|
+
Get
|
79
|
+
Return _Functions
|
80
|
+
End Get
|
81
|
+
Set
|
82
|
+
_Functions = Value
|
83
|
+
End Set
|
84
|
+
End Property
|
85
|
+
End Class
|
86
|
+
```
|
87
|
+
|
88
|
+
---
|
89
|
+
|
90
|
+
それとも厳密な`MVVM`の話なんですかね??
|
91
|
+
|
92
|
+
例題として`TreeView`はあんまり向きません(ややこしいので)が、厳密に`MVVM`するなら`Class Plugin`に対して`Class PluginViewModel`用意することになります。
|
93
|
+
|
94
|
+
[TreeView が配置された UI で Model-View-ViewModel パターンを使う方法(VB.NET) - 周回遅れのブルース](https://hilapon.hatenadiary.org/entry/20120111/1326273061)
|
95
|
+
上記リンクでいうと`DirectoryViewModel`が`PluginViewModel`で、`DirectoryInfo`が`Plugin`といった感じですかね?
|
96
|
+
|
97
|
+
今回のような単純な例の場合は、**モデルを直接ビューに公開する**ことはよくあります。
|
98
98
|
**そのため`Name`をプロパティにする**ということになります。
|
2
全面的に書き直し
answer
CHANGED
@@ -1,17 +1,66 @@
|
|
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
|
|
3
|
-
|
10
|
+
とりあえず期待通りに動かすコードはこうなります。
|
4
11
|
```xaml
|
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">
|
18
|
+
<Grid>
|
5
|
-
<TreeView x:Name="TreeView" ItemsSource="{Binding PluginEntries}">
|
19
|
+
<TreeView x:Name="TreeView" ItemsSource="{Binding PluginEntries}">
|
6
|
-
|
20
|
+
<TreeView.ItemTemplate>
|
7
|
-
|
21
|
+
<HierarchicalDataTemplate ItemsSource="{Binding Path=Functions}">
|
8
|
-
|
22
|
+
<TextBlock Text="{Binding Path=Name}" />
|
9
|
-
|
23
|
+
</HierarchicalDataTemplate>
|
10
|
-
|
24
|
+
</TreeView.ItemTemplate>
|
11
|
-
</TreeView>
|
25
|
+
</TreeView>
|
26
|
+
</Grid>
|
27
|
+
</Window>
|
12
28
|
```
|
13
29
|
|
14
30
|
```VB
|
31
|
+
Class MainWindow
|
32
|
+
Sub New()
|
33
|
+
Me.DataContext = New ViewModel
|
34
|
+
End Sub
|
35
|
+
|
36
|
+
End Class
|
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
|
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
|
63
|
+
|
15
64
|
Public Class Plugin
|
16
65
|
Private _Name As String
|
17
66
|
Private _Functions As List(Of Plugin)
|
@@ -34,4 +83,16 @@
|
|
34
83
|
End Set
|
35
84
|
End Property
|
36
85
|
End Class
|
37
|
-
```
|
86
|
+
```
|
87
|
+
|
88
|
+
---
|
89
|
+
|
90
|
+
それとも厳密な`MVVM`の話なんですかね??
|
91
|
+
|
92
|
+
例題として`TreeView`はあんまり向きません(ややこしいので)が、厳密に`MVVM`するなら`Class Plugin`に対して`Class PluginViewModel`用意することになります。
|
93
|
+
|
94
|
+
[TreeView が配置された UI で Model-View-ViewModel パターンを使う方法(VB.NET) - 周回遅れのブルース](https://hilapon.hatenadiary.org/entry/20120111/1326273061)
|
95
|
+
上記リンクでいうと`DirectoryViewModel`が`PluginViewModel`で、`DirectoryInfo`が`Plugin`といった感じですかね?
|
96
|
+
|
97
|
+
今回のような単純な例の場合は、**モデルを直接ビューに公開する**ことはよくあります。
|
98
|
+
**そのため`Name`をプロパティにする**ということになります。
|
1
VBコード
answer
CHANGED
@@ -9,4 +9,29 @@
|
|
9
9
|
</HierarchicalDataTemplate>
|
10
10
|
</TreeView.ItemTemplate>
|
11
11
|
</TreeView>
|
12
|
+
```
|
13
|
+
|
14
|
+
```VB
|
15
|
+
Public Class Plugin
|
16
|
+
Private _Name As String
|
17
|
+
Private _Functions As List(Of Plugin)
|
18
|
+
|
19
|
+
Public Property Name As String
|
20
|
+
Get
|
21
|
+
Return _Name
|
22
|
+
End Get
|
23
|
+
Set
|
24
|
+
_Name = Value
|
25
|
+
End Set
|
26
|
+
End Property
|
27
|
+
|
28
|
+
Public Property Functions As List(Of Plugin)
|
29
|
+
Get
|
30
|
+
Return _Functions
|
31
|
+
End Get
|
32
|
+
Set
|
33
|
+
_Functions = Value
|
34
|
+
End Set
|
35
|
+
End Property
|
36
|
+
End Class
|
12
37
|
```
|