【问题标题】:WP7 Databinding to Listbox from code with DataTemplateWP7 使用 DataTemplate 从代码中将数据绑定到列表框
【发布时间】:2011-11-20 02:07:29
【问题描述】:

这是我用来显示列表框的 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>

问题是数据绑定必须在代码中创建,因为每次都有不同的数据。 (事实上​​,数据是通过在 sql 中使用带有动态选择字段的 indexer[] 来的)。我现在的问题是如何将数据绑定应用到代码中的列表框和数据模板。据我所见,无法访问在 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);

【问题讨论】:

    标签: data-binding windows-phone-7 dynamic listbox


    【解决方案1】:

    我正要给你一个简单的例子,但我认为有很多更好的例子,所以请看看这些网站,看看它是否有帮助: WP7 - listbox Binding

    listbox-data-binding

    如果有什么不清楚的地方请评论。

    【讨论】:

    • 我很抱歉,但我认为你根本没有阅读我的问题。我不知道编译时的绑定...我必须在运行时生成绑定,而且我还必须在列表框上使用数据模板
    • 对不起。我不清楚您要做什么,如果有帮助,请尝试:windowsphonegeek.com/tips/WP7-ListBox-SelectedItem
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    相关资源
    最近更新 更多