Xaml WP7-使用listbox动态创建数据透视项

Xaml WP7-使用listbox动态创建数据透视项,xaml,windows-phone-7,listbox,pivot,Xaml,Windows Phone 7,Listbox,Pivot,我在MainPage.xaml中有以下代码: <controls:PivotItem Header="first"> <ListBox x:Name="MyListBox" Margin="0,0,-12,0" ItemsSource="{Binding ListBoxItem}"> <ListBox.ItemTemplate> <DataTemplate> <S

我在MainPage.xaml中有以下代码:

<controls:PivotItem Header="first">
    <ListBox x:Name="MyListBox" Margin="0,0,-12,0" ItemsSource="{Binding ListBoxItem}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Height="132">
                    <TextBlock Text="{Binding text}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</controls:PivotItem>


我需要用这样的模型在运行时创建N个数据透视项。我该怎么做呢?

让您的数据透视项delaration成为一个静态资源

<UserControl.Resources>

<DataTemplate x:Key="MyPivotItemTemplate">
   <controls:PivotItem Header="first" >
                <ListBox x:Name="MyListBox" Margin="0,0,-12,0" ItemsSource="{Binding ListBoxItem}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Height="132">
                                <TextBlock Text="{Binding text}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </controls:PivotItem>
</DataTemplate>

</UserControl.Resources>

然后在Pivot声明中,将其用作项目的模板

<Pivot .... ItemsTemplate="{StaticResource MyPivotItemTemplate}" Items="{Binding MyCollectionInDataContext}"

有一种比定义
用户控件
并找出该控件的绑定更简单的方法

这里的大部分复杂性都在
ItemTemplate
中-将
ItemTemplate
移动到该页面的ResourceDictionary中,并将其应用到所有列表框中。如果在许多页面/控件中使用该模板,甚至可以将其移动到App.xaml

<phone:PhoneApplicationPage.Resources>
...
    <DataTemplate x:Key="MyItemDataTemplate">
        <StackPanel Orientation="Horizontal" Height="132">
            <TextBlock Text="{Binding text}" />
        </StackPanel>
    </DataTemplate>
...
</phone:PhoneApplicationPage.Resources>

...
...
在设计时,您只需在每个枢轴项目中调用:

<controls:PivotItem Header="first">
    <ListBox x:Name="MyListBox" 
      Margin="0,0,-12,0" 
      ItemsSource="{Binding ListBoxItems}"
      ItemTemplate="{StaticResource MyItemDataTemplate}"/>
</controls:PivotItem>
<controls:PivotItem Header="second">
    <ListBox x:Name="MyListBox2" 
      Margin="0,0,-12,0" 
      ItemsSource="{Binding OtherListBoxItems}"
      ItemTemplate="{StaticResource MyItemDataTemplate}"/>
</controls:PivotItem>


如果您需要在运行时从代码中执行此操作,您可以从页面的
ResourceDictionary
中提取“MyItemDataTemplate”
ItemTemplate
对象,并将其应用于您创建的新列表框。

我今天确实做了类似的操作。您可以将
数据模板
模型应用于
数据透视项
和出现在
数据透视项
中的
列表框
。试试这个:

<controls:Pivot Name="PivotControl" ItemsSource="{Binding PivotItemBinding}">
    <controls:Pivot.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="Put your header bindings here"/>
        </DataTemplate>
     </controls:Pivot.HeaderTemplate>
     <controls:Pivot.ItemTemplate>
        <DataTemplate>
            <ListBox ItemsSource="{Binding ListBoxItem}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="132">
                          <TextBlock Text="{Binding text}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
           </ListBox>
       </DataTemplate>
   </controls:Pivot.ItemTemplate>
</controls:Pivot>


在这段代码中,将为绑定到它的每个项目创建一个
数据透视项
,其对应的
列表框
将填充来自同一ItemSource中集合的数据。

首先,我创建了一个用户控件

用户控制

 <UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="600" d:DesignWidth="480" Background="Transparent">

 <Grid x:Name="LayoutRoot" Background="Transparent">

        <ListBox x:Name="listUser" Margin="20,0,0,0" ScrollViewer.VerticalScrollBarVisibility="Visible"/>

</Grid>

         </UserControl>
                pivotItem = new PivotItem();
                pivotItem.Header = "Pivot";

                UserControl user = new UserControl();
                user.DataContext = list;
                user.listUser.ItemsSource = list;

                pivotItem.Content = null;
                pivotItem.Content = user;
                pivotfather.Items.Add(pivotItem);