WP7 - 使用列表框动态创建 PivotItem

发布于 2024-12-05 10:41:55 字数 602 浏览 1 评论 0原文

我在 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 个 PivotItem。我该怎么做呢?

I have this code in 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>

And I need to create N PivotItem's in runtime with such model. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伪装你 2024-12-12 10:41:55

让您的 PivotItem 声明成为静态资源。

<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}"

这样,每次您向 MyCollectionInDataContext 添加内容时,都会根据您定义的模板创建一个 PivotItem

Let your PivotItem delaration be a Static Resource.

<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>

Then in your Pivot declaration use it as a template for your items.

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

This way, each time you add something to a MyCollectionInDataContext, a PivotItem is created according to your defined template.

七禾 2024-12-12 10:41:55

我今天实际上做了类似的事情。您可以将 DataTemplate 模型应用于 PivotItemPivotItem 中显示的 ListBox。尝试一下:

<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>

在此代码中,将为绑定到它的每个项目创建一个 PivotItem,并且其相应的 ListBox 将使用来自集合中的数据填充相同的项目源。

I actually did something similar today. You can apply the DataTemplate model to both the PivotItem and the ListBox that appears in the PivotItem. Try this out:

<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>

In this code, a PivotItem will be created for each item you bind to it, and its corresponding ListBox will be populated with the data from the collection in the same ItemSource.

安穩 2024-12-12 10:41:55

有一种比定义 UserControl 并找出其绑定更简单的方法...

这里的大部分复杂性都在 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 对象/code> 并将其应用到您创建的新列表框。

There's an easier way than defining a UserControl and figuring out the binding for that...

Most of the complexity here is in the ItemTemplate - move the ItemTemplate into the ResourceDictionary for that page and apply it to all your ListBoxes. You can even move the template to App.xaml if you use it in many pages/controls.

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

At design-time you would simply call this up in each pivot item:

<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>

If you need to do this at runtime from code, you can pull the "MyItemDataTemplate" ItemTemplate object from the page's ResourceDictionary and apply it to the new ListBox you create.

执手闯天涯 2024-12-12 10:41:55

首先我

创建了一个用户控件UserControl

 <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>

在 xaml 中

。CS

                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);

first i created a user control

UserControl

 <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>

after in xaml

.cs

                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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文