如何在 UWP 中动态地将 PageResource 中的 GridView 添加到 pivotItem?

How to add a GridView from PageResource to a pivotItem dynamically in UWP?

我正在尝试在 c# 端动态创建多个 PivotItem,然后对于每个 PivotItem,我希望将内容设置为 GridView,它具有一个动态绑定元素的 Datatemplate。

XAML代码:

<Page.Resources>
<GridView x:Key="pivoteItemGV" 
    Margin="18,10,0,0"
     Background="#191919"
     SelectionMode="None">
    <GridView.ItemContainerStyle>
        <Style TargetType="GridViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
        </Style>
    </GridView.ItemContainerStyle>
    <GridView.ItemTemplate>
        <DataTemplate>
            <StackPanel MaxWidth="300">
                <TextBlock x:Name="ItemNameTB" Text="{Binding ItemName}"/>
                <TextBlock x:Name="ItemDescriptionTB" Text="{Binding ItemDescription}" 
                           TextWrapping="WrapWholeWords"/>
                <TextBlock x:Name="ItemPriceTB" Text="{Binding ItemPrice}" />
            </StackPanel>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>
</Page.Resources>

在 C# 端

private void BindTheContentToPage()
{
    foreach(var categoryItem in contentResourceDictionary)
    {
        PivotItem categoryPivotItem = new PivotItem();
        categoryPivotItem.Header = categoryItem.Key;
        GridView gv = this.Resources["pivoteItemGV"] as GridView;
        categoryPivotItem.Content = gv;
        categoryPivotItem.DataContext = categoryItem.Value;
        mainContentPivot.Items.Add(categoryPivotItem);
    }
}

应用程序在 categoryPivotItem.Content = gv; 崩溃,指出 值不在预期范围内。

我做的对吗?由于我不确定 Page.Resource 内容是否有多个副本,因此在本例中 GridView 是重复的。

提前感谢您的帮助。

  1. 不要像您那样将视觉元素直接放入 Resources 部分。你可以把 DataTemplate 放在那里。
  2. 您需要将 PivotItem.ContentTemplate 属性 设置为某些 DataTemplate。并将 PivotItem.Content 属性 设置为您要绑定的数据对象。

XAML代码:

<Page.Resources>
 <DataTemplate x:Key="pivoteItemGVTemplate">
   <GridView 
     ItemsSource="{Binding}"
     Margin="18,10,0,0"
     Background="#191919"
     SelectionMode="None">
    <GridView.ItemContainerStyle>
        <Style TargetType="GridViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
        </Style>
    </GridView.ItemContainerStyle>
    <GridView.ItemTemplate>
        <DataTemplate>
            <StackPanel MaxWidth="300">
                <TextBlock x:Name="ItemNameTB" Text="{Binding ItemName}"/>
                <TextBlock x:Name="ItemDescriptionTB" Text="{Binding ItemDescription}" 
                           TextWrapping="WrapWholeWords"/>
                <TextBlock x:Name="ItemPriceTB" Text="{Binding ItemPrice}" />
            </StackPanel>
        </DataTemplate>
    </GridView.ItemTemplate>
  </GridView>
 </DataTemplate>
</Page.Resources>

在 C# 代码隐藏中:

private void BindTheContentToPage()
{
    foreach(var categoryItem in contentResourceDictionary)
    {
        PivotItem categoryPivotItem = new PivotItem();
        categoryPivotItem.Header = categoryItem.Key;
        var template = this.Resources["pivoteItemGVTemplate"] as DataTemplate;
        categoryPivotItem.ContentTemplate = template;
        categoryPivotItem.Content = categoryItem.Value;
        mainContentPivot.Items.Add(categoryPivotItem);
    }
}

如果您的 categoryItem.Value 是一个数据对象列表,则向 GridView 添加一个属性(就像我在 XAML 中所做的那样):

ItemsSource="{Binding}"