Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding Text within Pivot.HeaderTemplate
    text
    copied!<p>I'm totally confused after trying to solve my problem for several hours for myself, but there are just too many question marks. Maybe you can help me :-)</p> <p>I want to have a Pivot and binding Elements to is, so I wrote a PivotViewModel class that contains an ObservableCollection (<code>CategoryPageList</code>):</p> <pre><code>public class PivotViewModel { public class CategoryPageList : ObservableCollection&lt;CategoryPage&gt; {} private CategoryPageList categoryPages; public PivotViewModel() { categoryPages = new CategoryPageList(); } public CategoryPageList CategoryPages { get { return categoryPages; } } } </code></pre> <p>The <code>CategoryPageList</code> is of the type <code>CategoryPage</code>, a class that contains the data I want to bind to, the "CategoryName":</p> <pre><code>public class CategoryPage : INotifyPropertyChanged { private string categoryName; public CategoryPage(string categoryName) { CategoryName = categoryName; } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public string CategoryName { get { return categoryName; } set { if (categoryName != value) { categoryName = value; NotifyPropertyChanged("CategoryName"); } } } } </code></pre> <p><strong>When my Application is loaded, I want to see a PivotItem for each CategoryPage with the "CategoryName"-Property as header of this PivotItem.</strong> With Expression Blend I created the following XAML-code, but it does not work, the Pivot remains empty:</p> <pre><code>&lt;Grid x:Name="LayoutRoot" Background="Transparent"&gt; &lt;Grid.DataContext&gt; &lt;RssReader_ViewModel:PivotViewModel/&gt; &lt;/Grid.DataContext&gt; &lt;controls:Pivot Title="pivot" ItemsSource="{Binding CategoryPages}"&gt; &lt;controls:Pivot.Resources&gt; &lt;DataTemplate x:Key="CategoryPivotItemHeaderTemplate"&gt; &lt;TextBlock TextWrapping="Wrap" Text="{Binding CategoryPages[0].CategoryName}"/&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="CategoryPivotItemTemplate"&gt; &lt;Grid&gt; &lt;ListBox/&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/controls:Pivot.Resources&gt; &lt;controls:Pivot.ItemTemplate&gt; &lt;StaticResource ResourceKey="CategoryPivotItemTemplate"/&gt; &lt;/controls:Pivot.ItemTemplate&gt; &lt;controls:Pivot.HeaderTemplate&gt; &lt;StaticResource ResourceKey="CategoryPivotItemHeaderTemplate"/&gt; &lt;/controls:Pivot.HeaderTemplate&gt; &lt;controls:Pivot.DataContext&gt; &lt;RssReader_ViewModel:PivotViewModel/&gt; &lt;/controls:Pivot.DataContext&gt; &lt;/controls:Pivot&gt; &lt;/Grid&gt; </code></pre> <p>The DataContext is bound in code in the constructor of the Page:</p> <pre><code>public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); LayoutRoot.DataContext = App.PivotViewModel; } } </code></pre> <p>I'm a total beginner and do not have much idea, but the only thing I can say is, that the in XAML the <code>Text="{Binding CategoryPages[0].CategoryName}"</code> looks strange to me.</p> <p>Does anybody see a bug? Would be really great! Best regards!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload