回答編集履歴
3
見直しキャンペーン中
answer
CHANGED
@@ -18,10 +18,9 @@
|
|
18
18
|
|
19
19
|
---
|
20
20
|
|
21
|
-
元コードを実行したときに、
|
21
|
+
元コードを実行したときに、↓のようなエラーが出力ウィンドウに大量に出ていたと思います。
|
22
22
|
```
|
23
23
|
System.Windows.Data Error: 40 : BindingExpression path error: 'people' property not found on 'object' ''Person' (HashCode=35943562)'. BindingExpression:Path=people.Age; DataItem='Person' (HashCode=35943562); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
|
24
24
|
```
|
25
|
-
|
25
|
+
|
26
|
-
「`Person`には`people`プロパティがないので、`TextBlock`の`Text`に値をセットできません。」のような意味なので、
|
26
|
+
「`Person`には`people`プロパティがないので、`TextBlock`の`Text`に値をセットできません。」のような意味なので、「あれ?ここには`Person`が入ってきているのか!」と気付くことができます。
|
27
|
-
「あれ?ここには`Person`が入ってきているのか!」と気付くことができます。
|
2
見直しキャンペーン中
answer
CHANGED
@@ -1,29 +1,27 @@
|
|
1
|
-
`DataContext`は基本的に親(上位のコントロール)の値を引き継ぎますが、`ListBox`のようにコレクションをソースにとるものの場合、下位の`ListBoxItem`にはコレクション中の1個が`DataContext`にセットされます。
|
2
|
-
|
3
|
-
`ItemTemplate`ではすでに個々の`Person`になっているため、↓こういうことになります。
|
4
|
-
```
|
5
|
-
<ListBox ItemsSource="{Binding people}">
|
6
|
-
<ListBox.ItemTemplate>
|
7
|
-
<DataTemplate>
|
8
|
-
<StackPanel>
|
9
|
-
<TextBlock Text="{Binding Age}" />
|
10
|
-
<TextBlock Text="{Binding Name}" />
|
11
|
-
</StackPanel>
|
12
|
-
</DataTemplate>
|
13
|
-
</ListBox.ItemTemplate>
|
14
|
-
</ListBox>
|
15
|
-
```
|
16
|
-
|
17
|
-
変更後のコードも`this.DataContext = this;`としたとすると同じになります。
|
18
|
-
|
19
|
-
---
|
20
|
-
|
21
|
-
元コードを実行したときに、
|
22
|
-
```
|
23
|
-
System.Windows.Data Error: 40 : BindingExpression path error: 'people' property not found on 'object' ''Person' (HashCode=35943562)'. BindingExpression:Path=people.Age; DataItem='Person' (HashCode=35943562); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
|
24
|
-
```
|
25
|
-
のようなエラーが出力ウィンドウに大量に出ていたと思います。
|
26
|
-
「`Person`には`people`プロパティがないので、`TextBlock`の`Text`に値をセットできません。」
|
27
|
-
の
|
28
|
-
「あれ?ここには`Person`が入ってきているのか!」
|
29
|
-
と気付くことができます。
|
1
|
+
`DataContext`は基本的に親(上位のコントロール)の値を引き継ぎますが、`ListBox`のようにコレクションをソースにとるものの場合、下位の`ListBoxItem`にはコレクション中の1個が`DataContext`にセットされます。
|
2
|
+
|
3
|
+
`ItemTemplate`ではすでに個々の`Person`になっているため、↓こういうことになります。
|
4
|
+
```xml
|
5
|
+
<ListBox ItemsSource="{Binding people}">
|
6
|
+
<ListBox.ItemTemplate>
|
7
|
+
<DataTemplate>
|
8
|
+
<StackPanel>
|
9
|
+
<TextBlock Text="{Binding Age}" />
|
10
|
+
<TextBlock Text="{Binding Name}" />
|
11
|
+
</StackPanel>
|
12
|
+
</DataTemplate>
|
13
|
+
</ListBox.ItemTemplate>
|
14
|
+
</ListBox>
|
15
|
+
```
|
16
|
+
|
17
|
+
変更後のコードも`this.DataContext = this;`としたとすると同じになります。
|
18
|
+
|
19
|
+
---
|
20
|
+
|
21
|
+
元コードを実行したときに、
|
22
|
+
```
|
23
|
+
System.Windows.Data Error: 40 : BindingExpression path error: 'people' property not found on 'object' ''Person' (HashCode=35943562)'. BindingExpression:Path=people.Age; DataItem='Person' (HashCode=35943562); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
|
24
|
+
```
|
25
|
+
のようなエラーが出力ウィンドウに大量に出ていたと思います。
|
26
|
+
「`Person`には`people`プロパティがないので、`TextBlock`の`Text`に値をセットできません。」のような意味なので、
|
27
|
+
「あれ?ここには`Person`が入ってきているのか!」と気付くことができます。
|
1
TextBlockのTextに値を
answer
CHANGED
@@ -23,7 +23,7 @@
|
|
23
23
|
System.Windows.Data Error: 40 : BindingExpression path error: 'people' property not found on 'object' ''Person' (HashCode=35943562)'. BindingExpression:Path=people.Age; DataItem='Person' (HashCode=35943562); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
|
24
24
|
```
|
25
25
|
のようなエラーが出力ウィンドウに大量に出ていたと思います。
|
26
|
-
「`Person`には`people`プロパティがないので、`TextBlock`
|
26
|
+
「`Person`には`people`プロパティがないので、`TextBlock`の`Text`に値をセットできません。」
|
27
27
|
のような意味なので、
|
28
28
|
「あれ?ここには`Person`が入ってきているのか!」
|
29
29
|
と気付くことができます。
|