3

I'm doing WP7 app using Panorama control and have a problem with binding into Panorama Title property. Is it possible to bind that value out from ViewModel object?

Binding in xaml file:

<controls:Panorama x:Name="prmPanorama" Title="{Binding Voyage.Title}">

Voyage property of ViewModel is a Model entity (with Title property inside) with OnNotifyPropertyChanged event fired every time it changes:

private Voyage _voyage;
public Voyage Voyage
{
    get { return _voyage; }
    set
    {
        if (_voyage != value)
        {
            _voyage = value;
            OnNotifyPropertyChanged("Voyage");
        }
    }
}

When I bind the same property into another control, eg. TextBlock, binding works just fine:

<TextBlock Text="{Binding Voyage.Title}" />

The text shown in that text block is as it should be but on the same time panorama title is not binded right - it's collapsed.

Does anyone tried to do that kind of binding? I have no idea why it doesn't work.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263

1 Answers1

1
    <DataTemplate x:Key="TitleDataTemplate"> 
       <TextBlock Text="{Binding}" /> 
    </DataTemplate>
    ... 
    <controls:Panorama Title="{Binding Voyage.Title}" 
                       TitleTemplate="{StaticResource TitleDataTemplate}">

The control template of the panorama control uses a content presenter to display whatever value the its title property has kind of like a button. When setting the title template property, you indirectly set the content template of the content presenter.

That is why you have to set the title property on the panorama control and then can use that value in your title template for binding. In other words its not enough to just bind to the title you have to give it a template.

Check out this link for more info

Edward
  • 7,346
  • 8
  • 62
  • 123
  • Doesn't work. Panorama Title is still collapsed and no text is displayed above PanoramaItems titles. Is it possible that when View is initialized and that Voyage property is not set (it is loaded from sql ce as a result of some queryString data retrieved while OnNavigatedTo event) whole Panorama Title layer is removed from the UI and it's not bindable later? – MarcinJuraszek Feb 13 '12 at 22:04
  • It shouldn't..I just tried it right now and I had no issues binding a string to the panorama title. Perhaps you can show your view model as well as when the view model is created? – Edward Feb 13 '12 at 23:04
  • The ViewModel is created as a field in View class and then assigned into DataContext in contrustor, just after InitializeComponents(). I copied some of code from both View and ViewModel here: http://pastebin.com/FNR9P6H2 – MarcinJuraszek Feb 14 '12 at 07:43