使用Pivot.ItemTemplate来避免复制

时间:2015-09-28 03:26:32

标签: xaml windows-runtime

以下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">
            <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 个答案:

答案 0 :(得分:1)

您无法设置ItemTemplate并以这种方式添加PivotItems。您需要通过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; }
}