teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

見直しキャンペーン中

2023/08/11 08:45

投稿

TN8001
TN8001

スコア10108

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

見直しキャンペーン中

2023/07/22 08:16

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -1,29 +1,27 @@
1
- `DataContext`は基本的に親(上位のコントロール)の値を引き継ぎますが、`ListBox`のようにコレクションをソースにとるものの場合、下位の`ListBoxItem`にはコレクション中の1個が`DataContext`にセットされます。
2
-
3
- `ItemTemplate`ではすでに個々の`Person`になっているため、↓こういうことになります。
4
- ```xaml
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に値を

2020/07/01 09:39

投稿

TN8001
TN8001

スコア10108

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``Text`をセットできません。」
26
+ 「`Person`には`people`プロパティがないので、`TextBlock``Text`に値をセットできません。」
27
27
  のような意味なので、
28
28
  「あれ?ここには`Person`が入ってきているのか!」
29
29
  と気付くことができます。