2010-12-20 7 views
0

Howdy、 ピボット要素内に複数のPivotItemを生成したいと思います。世代は進んでいますが、ピボット要素にどのようにテンプレートを適用できるのか分かりません。生成されたピボット要素をデータで埋め込む方法

私が使用する必要があるだろうと思った:

pivotItem.ContentTemplate =(DataTemplateを)Ressources [ "KantinenUebersicht"];

しかし、空のページしか作成されていません。

ressourceファイル内の私のコードは次のようになります:

は私の全体のressourcesが

< .ResourceDictionary のxmlns = "http://schemas.microsoft.com/winfx/2006/ファイルの挿入しましたXAML /プレゼンテーション」 のxmlns:X = "http://schemas.microsoft.com/winfx/2006/xaml">

<DataTemplate x:Key="KantinenTemplate"> 
    <StackPanel VerticalAlignment="Top"> 
      <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding name}" FontSize="{StaticResource PhoneFontSizeLarge}" Margin="8,0,0,0" VerticalAlignment="Top"/> 
      <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding entfernung}" Margin="24,0,0,0" FontSize="{StaticResource PhoneFontSizeSmall}" VerticalAlignment="Bottom"/> 
    </StackPanel> 
</DataTemplate> 

<DataTemplate x:Key="KantinenUebersicht"> 
     <Grid> 
      <TextBlock x:Name="KantinenName" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Text="" RenderTransformOrigin="0.566,0.407" Margin="0,0,8,0" Height="55" VerticalAlignment="Top"> 
         <TextBlock.Foreground> 
          <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/> 
         </TextBlock.Foreground> 
      </TextBlock> 
      <TextBlock x:Name="sdfsdf" HorizontalAlignment="Left" Height="40" Margin="8,59,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="196"/> 
      <TextBlock x:Name="lblGeoefsdfsdffnet" Height="40" Margin="208,59,8,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/> 
     </Grid> 
    </DataTemplate></ResourceDictionary.> 

をしなければなりませんでした挿入する。最初のタグに...

答えて

1

あなたがしようとしているのは正しいです。

あなたの質問には、 "リソース"に余分な "S"があります。その後、

<phone:PhoneApplicationPage.Resources> 
    <DataTemplate x:Key="KantinenUebersicht"> 
     <Grid> 
      <TextBlock x:Name="KantinenName" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Text="" RenderTransformOrigin="0.566,0.407" Margin="0,0,8,0" Height="55" VerticalAlignment="Top"> 
       <TextBlock.Foreground> 
        <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/> 
       </TextBlock.Foreground> 
      </TextBlock> 
      <TextBlock x:Name="lblEntfernung" HorizontalAlignment="Left" Height="40" Margin="8,59,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="196"/> 
      <TextBlock x:Name="lblGeoeffnet" Height="40" Margin="208,59,8,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/> 
     </Grid> 
    </DataTemplate> 
</phone:PhoneApplicationPage.Resources> 

と新しいアイテムを作成するときに、それを使用します:

私はあなたの質問からコードを取る場合、私はページに追加することができ、私のマシン上で

var newItem = new PivotItem { Header = "Added" }; 

newItem.ContentTemplate = (DataTemplate)Resources["KantinenUebersicht"]; 

MyPivot.Items.Add(newItem); 

ワークス(エミュレータ)。

更新:
あなたが好きそうすることができる別のファイルにリソースを追加する場合:

ResourceDictionary.xaml

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > 
    <DataTemplate x:Key="KantinenUebersicht"> 
     <Grid> 
      <TextBlock x:Name="KantinenName" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Text="" RenderTransformOrigin="0.566,0.407" Margin="0,0,8,0" Height="55" VerticalAlignment="Top"> 
        <TextBlock.Foreground> 
         <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/> 
        </TextBlock.Foreground> 
      </TextBlock> 
      <TextBlock x:Name="lblEntfernung" HorizontalAlignment="Left" Height="40" Margin="8,59,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="196"/> 
      <TextBlock x:Name="lblGeoeffnet" Height="40" Margin="208,59,8,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/> 
     </Grid> 
    </DataTemplate> 
</ResourceDictionary> 

次にページで、この外部ファイルを参照する必要がありますこのファイルを使用したいと考えています。

<phone:PhoneApplicationPage.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="\ResourceDictionary.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</phone:PhoneApplicationPage.Resources> 
+0

実際にはリソースの2番目の「s」はボックスに入力中に起こるエラーでした。しかし、欠落しているのは ''ですが、それを自分のressourcesファイルに追加すると、エラーが返されます。 – theXs

+0

@theXsあなたの "ressources file"は何ですか?上記の私のコードサンプルは、MainPage.xamlのものです。 –

+0

私のRessourceFile名は今まで "ResourceDictionary1.xaml"です。私はデバッグし、 "this.resources"の下に見えるものをチェックしようとしましたが、それは空の辞書です - なぜ私は出力が見えないのでしょうか? – theXs

関連する問題