![](/img/trans.png)
[英]Binding webService data to the ListBox DataTemplate WP7
[英]WP7 Databinding to Listbox from code with DataTemplate
这是我用来显示列表框的XAML:
<ListBox x:Name="groupedList" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<ListBox.ItemTemplate>
<DataTemplate x:Name="myTemplate">
<StackPanel Orientation="Vertical">
<TextBlock Grid.Row="0" FontWeight="Bold" x:Name="templateHeader" />
<TextBlock Grid.Row="1" x:Name="templateCaption"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
问题在于,由于每次都有不同的数据,因此必须在代码中创建数据绑定。 (实际上,数据是通过将indexer []与sql中动态选择的字段结合使用来实现的)。 我的问题现在是如何将代码中的数据绑定应用于列表框和数据模板。 据我所知,没有办法访问在DataTemplate内部定义的项目。
到目前为止,这是我的尝试:
Binding binding = new Binding("["+CaptionField+"]");
binding.Source = this.controller.table.Rows;
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(this.groupedList.Items, ItemsControl.ItemsSourceProperty, binding);
this.groupedList.SetBinding(null, binding);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.