質問編集履歴

1

質問追記

2017/08/21 06:14

投稿

kawauso
kawauso

スコア56

test CHANGED
File without changes
test CHANGED
@@ -51,3 +51,87 @@
51
51
 
52
52
 
53
53
  解決策をご存知の方、よろしくお願いいたします。
54
+
55
+
56
+
57
+ **追記:**
58
+
59
+ 立て続けに質問、申し訳ございません。
60
+
61
+ たとえば、下記のようなViewModelにIndexを振りたい場合はどのようにすればよろしいでしょうか。
62
+
63
+
64
+
65
+ ```C#
66
+
67
+ public class MyItem: BindableBase
68
+
69
+ {
70
+
71
+ public int Index { set; get; }
72
+
73
+ public string ItemName { set; get; }
74
+
75
+ :
76
+
77
+ }
78
+
79
+
80
+
81
+ private ObservableCollection<MyItem> MyItemCollection { set; get; } = new ObservableCollection<MyItem>()
82
+
83
+ {
84
+
85
+ new MyItem{ ItemName="item1" },
86
+
87
+ new MyItem{ ItemName="item2" },
88
+
89
+ new MyItem{ ItemName="item3" },
90
+
91
+ new MyItem{ ItemName="item4" },
92
+
93
+ new MyItem{ ItemName="item5" }
94
+
95
+ };
96
+
97
+ ```
98
+
99
+
100
+
101
+
102
+
103
+ ```XAML
104
+
105
+ <ListBox.ItemTemplate>
106
+
107
+
108
+
109
+ <DataTemplate>
110
+
111
+ <StackPanel Orientation="Horizontal">
112
+
113
+ <TextBlock Text="{Binding Index/>
114
+
115
+ <TextBlock Text=" : " />
116
+
117
+ <TextBlock Text="{Binding ItemName}" />
118
+
119
+ </StackPanel>
120
+
121
+ </DataTemplate>
122
+
123
+ </ListBox.ItemTemplate>
124
+
125
+ ```
126
+
127
+
128
+
129
+ 下記のように記述してみたところ、
130
+
131
+ ``<TextBlock Text="{Binding Path=Index, RelativeSource={RelativeSource AncestorType=ListBoxItem}, Converter={StaticResource ItemContainerToIndexConverter}}"/>``
132
+
133
+
134
+
135
+ このようなエラーが出力されます。
136
+
137
+ ``System.Windows.Data Error: 40 : BindingExpression path error: 'Index' property not found on 'object' ''ListBoxItem' (Name='')'. BindingExpression:Path=Index; DataItem='ListBoxItem' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')``