2009-01-26 5 views
7

dân sử dụng thường xuyên một cái gì đó như:Làm thế nào để ràng buộc trong WPF một đối tượng toàn bộ để kiểm soát người dùng?

<ListBox ItemsSource="{Binding ElementName=thisControl, Path=ListIndexes}"> 
<ListBox.ItemTemplate> 
<DataTemplate> 
<StackPanel> 
<Label Content="{Binding Path=IndexName}"/> 
<Label Content="{Binding Path=IndexValue}"/> 
</StackPanel> 
</DataTemplate> 
</ListBox.ItemTemplate> 
</ListBox> 

Nhưng tôi muốn sử dụng, thay vì nhãn, một điều khiển, như thế này:

<ListBox ItemsSource="{Binding ElementName=thisControl, Path=ListIndexes}"> 
<ListBox.ItemTemplate> 
<DataTemplate> 
<local:Index Item="**{Binding}**"/> 
</DataTemplate> 
</ListBox.ItemTemplate> 
</ListBox> 

nghi ngờ của tôi là những gì để đưa vào Binding này bao gồm toàn bộ mục từ bộ sưu tập.

Trả lời

13

Cú pháp cho việc này là:

<local:Index Item="{Binding}"/> 

này sẽ cho các dữ liệu chức năng ràng buộc để ràng buộc toàn bộ DataContext cho mỗi ListBox Item Item sở hữu trong kiểm soát Index của bạn