繁体   English   中英

WP7从具有DataTemplate的代码到列表框的数据绑定

[英]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);

我本来想给您提供一个简单的示例,但我认为有很多更好的示例,因此请查看以下站点以查看是否有帮助: WP7-列表框绑定

列表框数据绑定

如果不清楚,请发表评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM