回答編集履歴

3

見直しキャンペーン中

2023/08/11 08:45

投稿

TN8001
TN8001

スコア9326

test 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

見直しキャンペーン中

2023/07/22 08:16

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,57 +1,27 @@
1
1
  `DataContext`は基本的に親(上位のコントロール)の値を引き継ぎますが、`ListBox`のようにコレクションをソースにとるものの場合、下位の`ListBoxItem`にはコレクション中の1個が`DataContext`にセットされます。
2
2
 
3
-
4
-
5
3
  `ItemTemplate`ではすでに個々の`Person`になっているため、↓こういうことになります。
6
-
7
- ```xaml
4
+ ```xml
8
-
9
5
  <ListBox ItemsSource="{Binding people}">
10
-
11
6
  <ListBox.ItemTemplate>
12
-
13
7
  <DataTemplate>
14
-
15
8
  <StackPanel>
16
-
17
9
  <TextBlock Text="{Binding Age}" />
18
-
19
10
  <TextBlock Text="{Binding Name}" />
20
-
21
11
  </StackPanel>
22
-
23
12
  </DataTemplate>
24
-
25
13
  </ListBox.ItemTemplate>
26
-
27
14
  </ListBox>
28
-
29
15
  ```
30
-
31
-
32
16
 
33
17
  変更後のコードも`this.DataContext = this;`としたとすると同じになります。
34
18
 
35
-
36
-
37
19
  ---
38
20
 
39
-
40
-
41
21
  元コードを実行したときに、
42
-
43
22
  ```
44
-
45
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')
46
-
47
24
  ```
48
-
49
25
  のようなエラーが出力ウィンドウに大量に出ていたと思います。
50
-
51
- 「`Person`には`people`プロパティがないので、`TextBlock`の`Text`に値をセットできません。」
26
+ 「`Person`には`people`プロパティがないので、`TextBlock`の`Text`に値をセットできません。」のような意味なので、
52
-
53
- のような意味なので、
54
-
55
- 「あれ?ここには`Person`が入ってきているのか!」
27
+ 「あれ?ここには`Person`が入ってきているのか!」と気付くことができます。
56
-
57
- と気付くことができます。

1

TextBlockのTextに値を

2020/07/01 09:39

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
  のようなエラーが出力ウィンドウに大量に出ていたと思います。
50
50
 
51
- 「`Person`には`people`プロパティがないので、`TextBlock``Text`をセットできません。」
51
+ 「`Person`には`people`プロパティがないので、`TextBlock``Text`に値をセットできません。」
52
52
 
53
53
  のような意味なので、
54
54