DFBerry
DFBerry

Reputation: 2006

WP7 Pivot Title Template DataContext Binding not working

I have a Pivot Title Template so that the Pivot page has a title and subtitle. I want to set both via code.

I build the XAML in Blend and without code binding, it does display so that part works.

However, my binding isn't working. It either won't build because the object doesn't have a DataContext, or the object doens't exist in current context or does build but won't display. When it doesn't display, I assume I'm binding to the wrong XAML object.Each object is named only so I could find the right object to bind to.

The containing class for the bound code of Title and Subtitle looks like:

public class PivotTitle
{
    public string Title = "";
    public string Subtitle = "";
}

My question is: how do I correctly bind the TitleTemplate so that the two TextBlocks' Text properties can be set in code?

Here is the XAML

       <controls:Pivot.TitleTemplate >
            <DataTemplate x:Name="PivotTitleTemplateDataTemplate"  >
                <StackPanel x:Name="MyPivotTitle" DataContext="{Binding}" >
                    <TextBlock x:Name="Title"
                        Text="{Binding Title}"
                        FontSize="20"
                        TextWrapping="Wrap"/>
                    <TextBlock x:Name="Subtitle"
                        Text="{Binding Subtitle}"
                        Foreground="Gray"
                        TextWrapping="Wrap"/>
                </StackPanel>
            </DataTemplate>
        </controls:Pivot.TitleTemplate>

The title and subtitle are dependent on the page navigated from. The code behind looks like:

//defined at top of page class public PivotTitle _PivotTitle = new PivotTitle();

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);


        string queryStringDeploymentName = "";

        this.NavigationContext.QueryString.ContainsKey("DeploymentName"))

        _PivotTitle.Title = SelectedDeployment.DeploymentName;
        _PivotTitle.Subtitle = App.ViewModel.AppSettings.UpdatedText;
       MyPivotTitle.DataContext = _PivotTitle;
     }

This particular example won't build with this error: The name 'MyPivotTitle' does not exist in the current context. The binding on the Pivot page's Listbox is working correctly.

I believe my codebehind and overall XAML are correct. I think I'm DataContext binding incorrectly.

Thanks for any help.

Upvotes: 1

Views: 3016

Answers (1)

Derek Lakin
Derek Lakin

Reputation: 16319

In your example MyPivotTitle is an element within the DataTemplate, so cannot be accessed directly from your code, hence the error. If you need to access a control inside a template of an items control (such as Pivot or ListBox`) then you may find this article from WIndowsPhoneGeek.com useful.

However, it sounds like you might be able to do this without any "hacking". Is this title and subtitle just for a single pivot item, or will it be on all of them? What other data, if any are you binding on the pivot item(s)?

Upvotes: 2

Related Questions