2010-12-20 44 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