2015-09-28 95 views
0

下面的XAML工作確定:使用Pivot.ItemTemplate避免複製

<Page ... 

    <Page.Resources> 
     <SolidColorBrush x:Key="brushHeaderBkgr" Color="Blue" /> 
     <SolidColorBrush x:Key="brushContentBorder" Color="Aquamarine" /> 
     <SolidColorBrush x:Key="brushContentBkgr" Color="Blue" /> 
     <SolidColorBrush x:Key="brushContentList" Color="Red" /> 

     <Style TargetType="Border" x:Key="ContentFrame"> 
      <Setter Property="BorderBrush" Value="{StaticResource brushContentBorder}" /> 
      <Setter Property="BorderThickness" Value="6" /> 
      <Setter Property="Padding" Value="6" /> 
      <Setter Property="Background" Value="{StaticResource brushContentBkgr}" /> 
     </Style> 

     <Style TargetType="ListView" x:Key="ContentList" > 
      <Setter Property="Background" Value="{StaticResource brushContentList}" /> 
     </Style> 
    </Page.Resources> 

    <Grid> 
     <Pivot Title="Whatever" Margin="10"> 
      <PivotItem Header="Header1" > 
        <Border Style="{StaticResource ContentFrame}"> 
         <ListView x:Name="Content" Style="{StaticResource ContentList}" /> 
        </Border> 
      </PivotItem> 

      <PivotItem Header="Header2" > 
        <Border Style="{StaticResource ContentFrame}"> 
         <ListView x:Name="Content" Style="{StaticResource ContentList}" /> 
        </Border> 
      <PivotItem Header="Header3" > 
        <Border Style="{StaticResource ContentFrame}"> 
         <ListView x:Name="Content" Style="{StaticResource ContentList}" /> 
        </Border> 
     </Pivot> 

    </Grid> 
</Page> 

由於所有的商品頁面,具有相同的結構,我想用Pivot.ItemTemplate以避免複製。這裏是修改的XAML:

<Page ... 

    <Page.Resources> 
     <SolidColorBrush x:Key="brushHeaderBkgr" Color="Blue" /> 
     <SolidColorBrush x:Key="brushContentBorder" Color="Aquamarine" /> 
     <SolidColorBrush x:Key="brushContentBkgr" Color="Blue" /> 
     <SolidColorBrush x:Key="brushContentList" Color="Red" /> 

     <Style TargetType="Border" x:Key="ContentFrame"> 
      <Setter Property="BorderBrush" Value="{StaticResource brushContentBorder}" /> 
      <Setter Property="BorderThickness" Value="6" /> 
      <Setter Property="Padding" Value="6" /> 
      <Setter Property="Background" Value="{StaticResource brushContentBkgr}" /> 
     </Style> 

     <Style TargetType="ListView" x:Key="ContentList" > 
      <Setter Property="Background" Value="{StaticResource brushContentList}" /> 
     </Style> 
    </Page.Resources> 

    <Grid> 
     <Pivot Title="Whatever" Margin="10"> 
     <Pivot.ItemTemplate> 
      <DataTemplate> 
        <Border Style="{StaticResource ContentFrame}"> 
         <ListView x:Name="Content" Style="{StaticResource ContentList}" /> 
       </Border> 
      </DataTemplate> 
     </Pivot.ItemTemplate> 

     <PivotItem Header="Header1" /> 
     <PivotItem Header="Header2" /> 
     <PivotItem Header="Header3" /> 

    </Grid> 
</Page> 

但是,這不起作用,項目不會出現。爲什麼?

回答

1

您不能設置ItemTemplate並以這種方式添加數據透視項。您需要通過ItemSource將數據源添加到數據透視表並在項目模板中使用綁定。
看看這段代碼。

MainPage.xaml中:

<Pivot x:Name="Pivot" Title="Whatever" Margin="10"> 
    <Pivot.ItemTemplate> 
     <DataTemplate> 
      <Border Style="{StaticResource ContentFrame}"> 
       <ListView ItemsSource="{Binding ListViewSource}" Style="{StaticResource ContentList}" /> 
      </Border> 
     </DataTemplate> 
    </Pivot.ItemTemplate> 
    <Pivot.HeaderTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Header}"/> 
     </DataTemplate> 
    </Pivot.HeaderTemplate> 
</Pivot> 

MainPage.xaml.cs中:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    List<PivotModel> items = new List<PivotModel>(); 
    items.Add(new PivotModel() { Header = "Header1", ListViewSource = Enumerable.Range(10, 10).ToList() }); 
    items.Add(new PivotModel() { Header = "Header2", ListViewSource = Enumerable.Range(20, 10).ToList() }); 
    items.Add(new PivotModel() { Header = "Header3", ListViewSource = Enumerable.Range(30, 10).ToList() }); 
    items.Add(new PivotModel() { Header = "Header4", ListViewSource = Enumerable.Range(40, 10).ToList() }); 

    Pivot.ItemsSource = items; 
} 

PivotModel.cs:

public class PivotModel 
{ 
    public string Header { get; set; } 

    public List<int> ListViewSource { get; set; } 
}