简体   繁体   English

WP8将子标题文本添加到pivot控件头

[英]WP8 add subtitle text to pivot control header

I want to create custom pivot header with two textblocks: one for header title and one for subtitle or maybe add static subtitle same for every pivot page. 我想创建带有两个文本块的自定义数据透视表头:一个用于标题标题,另一个用于字幕,或者可以为每个数据透视页添加相同的静态字幕。 The easy way is to define header template like in example below, but how I can bind subtitle text to actual control to be able to change in in code? 简单的方法是定义标题模板,如下面的示例,但我如何将字幕文本绑定到实际控件,以便能够在代码中更改? If the only option is to overwrite pivot control, please provide some examples if exist. 如果唯一的选择是覆盖枢轴控制,请提供一些示例(如果存在)。

<phone:Pivot.HeaderTemplate>
    <DataTemplate>
         <Grid>
              <TextBlock Text="{Binding}" /> //header
              <TextBlock Text="{Binding}" /> //subtite
         </Grid>
    </DataTemplate>
</phone:Pivot.HeaderTemplate>

Thanks. 谢谢。

I succeed to do what you want but I'm not proud of the solution... 我成功地做了你想做的事,但我并不为解决方案感到骄傲......

First, I created an object that take 2 string : 首先,我创建了一个带2个字符串的对象:

public class Header
{
    public string Title { get; set; }
    public string Subtitle { get; set; }
}

Then a create the object in XAML in resources (or you can bind them through you viewmodel) 然后在资源中创建XAML中的对象(或者您可以通过viewmodel绑定它们)

<phone:PhoneApplicationPage.Resources>
    <poc:Header x:Key="FirstHeader" Title="first" Subtitle="first subtitle"/>
    <poc:Header x:Key="SecondHeader" Title="second" Subtitle="second subtitle"/>
</phone:PhoneApplicationPage.Resources>

Bind the objects on each of your pivotitem: 绑定每个pivotitem上的对象:

<phone:PivotItem Header="{StaticResource FirstHeader}">

Then style your Pivot.HeaderTemplate like this: 然后像这样设置你的Pivot.HeaderTemplate样式:

<phone:Pivot.HeaderTemplate>
    <DataTemplate>
        <StackPanel Orientation="Vertical">
            <TextBlock Text="{Binding Title}" Foreground="White"/>
            <TextBlock Text="{Binding Subtitle}" FontSize="18" Foreground="White"/>
        </StackPanel>
    </DataTemplate>
</phone:Pivot.HeaderTemplate>

A better solution can be achieved by modifying the behavior of the Pivot and PivotItem but I didn't succeed to change the binding between the Pivot.HeaderTemplate and the PivotItem. 通过修改Pivot和PivotItem的行为可以实现更好的解决方案,但是我没有成功更改Pivot.HeaderTemplate和PivotItem之间的绑定。

You could try and define the header for every item separately instead for the whole pivot. 您可以尝试单独为每个项目定义标题,而不是整个数据透视表。 In case you want to make it work for the whole pivot you should change the style, and extend it with subtitle properties. 如果你想让它适用于整个数据透视表,你应该改变样式,并用字幕属性扩展它。

Anyway here's the code if u wanna change the header for the pivot item only 无论如何,如果你想改变枢轴项目的标题,这里是代码

<controls:Pivot Name="MainPivot">
    <controls:PivotItem x:Name="HomeMenuPivotItem">
        <controls:PivotItem.Header>
            <StackPanel>
                <TextBlock Text="Title"/>
                <TextBlock Text="Subtitle"/>
            </StackPanel>
        </controls:PivotItem.Header>
    </controls:PivotItem>
</controls:Pivot>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM