Data binding 使用DataTemplate从代码将WP7数据绑定到Listbox

Data binding 使用DataTemplate从代码将WP7数据绑定到Listbox,data-binding,windows-phone-7,dynamic,listbox,Data Binding,Windows Phone 7,Dynamic,Listbox,下面是我用来显示列表框的XAML: <ListBox x:Name="groupedList" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > <ListBox.ItemTemplate> <DataTemplate x:Name="myTemplate"> <StackPanel Orien

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

我正要给你们举一个简单的例子,但我认为还有很多更好的例子,所以请看看这些网站,看看它是否有用:


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

很抱歉,我想你根本没有读过我的问题。我不知道编译时的绑定。。。我必须在运行时生成绑定,还必须使用ListBox上的datatemplates,对不起。我不清楚你想做什么,如果这有帮助,试试:
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);