2010-12-20 42 views
0

你好, 我想在我的應用程序中生成幾個PivotItems在pivotelement。這代人進行得很好,但我不知道如何將模板應用到這些樞軸元素。如何用數據填充生成的樞軸元素[模板]

我想我會需要使用:

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

實際上,第二個資源是在輸入框中發生的錯誤。但是缺少的是'',但是當我將其添加到我的資源文件時,它會返回錯誤 – theXs 2010-12-20 15:28:08

+0

@theXs什麼是您的「資源文件」?我上面的代碼示例來自MainPage.xaml。 – 2010-12-20 16:05:18

+0

我的RessourceFile名稱是至今「ResourceDictionary1.xaml」。我只是試圖調試和檢查「this.resources」下可以看到的內容......但它是一個空字典 - 這可能也意味着爲什麼我沒有看到任何輸出。 – theXs 2010-12-20 16:06:44