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

質問編集履歴

1

質問追記

2017/08/21 06:14

投稿

kawauso
kawauso

スコア56

title CHANGED
File without changes
body CHANGED
@@ -24,4 +24,46 @@
24
24
  1: item3
25
25
  2: item4
26
26
 
27
- 解決策をご存知の方、よろしくお願いいたします。
27
+ 解決策をご存知の方、よろしくお願いいたします。
28
+
29
+ **追記:**
30
+ 立て続けに質問、申し訳ございません。
31
+ たとえば、下記のようなViewModelにIndexを振りたい場合はどのようにすればよろしいでしょうか。
32
+
33
+ ```C#
34
+ public class MyItem: BindableBase
35
+ {
36
+ public int Index { set; get; }
37
+ public string ItemName { set; get; }
38
+ :
39
+ }
40
+
41
+ private ObservableCollection<MyItem> MyItemCollection { set; get; } = new ObservableCollection<MyItem>()
42
+ {
43
+ new MyItem{ ItemName="item1" },
44
+ new MyItem{ ItemName="item2" },
45
+ new MyItem{ ItemName="item3" },
46
+ new MyItem{ ItemName="item4" },
47
+ new MyItem{ ItemName="item5" }
48
+ };
49
+ ```
50
+
51
+
52
+ ```XAML
53
+ <ListBox.ItemTemplate>
54
+
55
+ <DataTemplate>
56
+ <StackPanel Orientation="Horizontal">
57
+ <TextBlock Text="{Binding Index/>
58
+ <TextBlock Text=" : " />
59
+ <TextBlock Text="{Binding ItemName}" />
60
+ </StackPanel>
61
+ </DataTemplate>
62
+ </ListBox.ItemTemplate>
63
+ ```
64
+
65
+ 下記のように記述してみたところ、
66
+ ``<TextBlock Text="{Binding Path=Index, RelativeSource={RelativeSource AncestorType=ListBoxItem}, Converter={StaticResource ItemContainerToIndexConverter}}"/>``
67
+
68
+ このようなエラーが出力されます。
69
+ ``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')``