2016-04-13 10 views
1

私はC#の最後に動的にみとめPivotItemsを作成しようとしているし、その後、すべてのPivotItemのために私はセットの要素が動的にバインドされたDataTemplateを持ったGridViewなどのコンテンツたいです。PageResourceからGridViewをpivotItemにUWPで動的に追加する方法は?

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が複製されます。

ご協力ありがとうございます。

答えて

2
  1. 視覚要素をそのままResourcesセクションに入れないでください。そこにDataTemplateを入れることができます。
  2. あなたは、いくつかのDataTemplatePivotItem.ContentTemplateプロパティを設定する必要があります。バインドするデータオブジェクトに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コントロールに属性を追加するには(IなどXAMLで行った):

ItemsSource="{Binding}" 
+0

これは私が探しているものです。しかし、ここではコンテンツテンプレートにデータテンプレートを割り当てていますが、category.valueには視覚的な要素が含まれていないため、コンテンツはありません。これを試みたが、それはあなたがあなたのGridViewののItemsSourceを設定しなかったので、多分それはです私の内容 – AbsoluteSith

+0

を表示doesntの。あなたのcategoryItem.Valueは、データオブジェクトの一覧がある場合は、GridViewコントロールに属性を追加します。 のItemsSource =「{}バインディング」。私はこの新しいラインで答えを編集しました – Artemious

+0

それはそれでした!ご協力いただきありがとうございます! – AbsoluteSith

関連する問題