我有一个Pivot Control
和三个PivotItems
加入Blend
。每个PivotItem
有ViewModel
,因为它是DataContext
。每个ViewModel
都有一个字符串属性Title
。我想编辑Header Template
,创建一个TextBlock
并将它的Text
属性绑定到Title
,但由于某些原因它不起作用。我确信这是因为Header Template
的DataContext
为空,但我认为它会从特定的PivotItem
继承。这是标题模板的xaml
代码。绑定枢轴页眉模板
<DataTemplate x:Key="HeaderTemplate">
<Grid Height="47" Width="354">
<TextBlock Margin="8,0,64,8" TextWrapping="Wrap" Text="{Binding Title}" d:LayoutOverrides="Width" FontSize="24" Foreground="Red"/>
</Grid>
</DataTemplate>
<controls:Pivot TitleTemplate="{StaticResource TitleTemplate}" HeaderTemplate="{StaticResource HeaderTemplate}">
<!--Pivot item one-->
<controls:PivotItem DataContext="{Binding Home, Mode=OneWay}">
<Grid>
<ListBox x:Name="listBox" ItemsPanel="{StaticResource HomeItemsPanel}" ItemTemplate="{StaticResource HomeItemTemplate}" ItemsSource="{Binding Tiles}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding OnSelectionChanged}" CommandParameter="{Binding SelectedIndex, ElementName=listBox}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
</Grid>
</controls:PivotItem>
<controls:PivotItem Margin="0,8,0,1">
<Grid/>
</controls:PivotItem>
<!--Pivot item two-->
<controls:PivotItem DataContext="{Binding More, Mode=OneWay}">
<Grid/>
</controls:PivotItem>
</controls:Pivot>
我想创建一个更复杂的Header Template
,但是,现在我正在测试,如果我可以绑定它的东西不仅仅是一个TextBlock更出发之前。我知道我可以使用List
的ViewModels
作为PivotControl.ItemSource
,但是之后我将不得不使用DataTemplateSelector
等等,我不想那样做。
所以我想我的问题是,我怎么能使HeaderTemplate继承根据PivotItem
的DataContext? 谢谢。
您是否尝试过使用元素绑定? – 2013-03-18 18:34:28
我不确定你的意思。 – Cosmin 2013-03-18 19:07:49