App Hub
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (7 posts)

Pivot control Items property manipulation

Last post 11/2/2010 6:33 PM by Devscope.. 6 replies.
  • 11/1/2010 11:46 AM

    Pivot control Items property manipulation


    Greetings,

    I'm currently having a huge problem with Pivot control (and as far I've seen, I'm not the only one with Pivot problems...)

    Let's say I have a Pivot with 5 PivotItems in it, and I'm now viewing the first one (so, the Pivot SelectedIndex property is 0).

    Then I press a button on the ApplicationBar that does Pivot Items.Clear() and the do 5 new PivotItems and add them with Items.Add() on the Pivot control -- no problem here

    Now, let's start again with the Pivot with 5 items, only now I want to view the last one (so that Pivot SelectedIndex property is 4)

    I now press the ApplicationBar button that does the Items.Clear() and start to add new Items -- ArgumentOutOfRangeException throws :(

    Even if I do SelectedIndex = 0 BEFORE I do the Items.Clear(), I always get the exception!!!


    Any thoughts over this one?

    Best regards to all!
  • 11/1/2010 2:08 PM In reply to

    Re: Pivot control Items property manipulation

    Hi
    Try set the 'SelectedIndex = -1' insted'
  • 11/1/2010 10:00 PM In reply to

    Re: Pivot control Items property manipulation

    Thank you for the reply Sigurd, but unfortunatly, that didn't work either!

    I've done a small sample code to illustrate the problem, you can get it here:


    To reproduce the problem, just change to an item with index different from 0, and click the button... the problems are so many that I've lost count!!! :(
  • 11/1/2010 10:32 PM In reply to

    Re: Pivot control Items property manipulation

    The problem is that the pivot control has a header collection where it keeps a separate SelectedIndex and when you clear out your item source, it isn't really thrilled about it as when the layout gets updated (when you add your new items), it can't find it's current selected header.

    A quick fix is to first zero out your DataContext and then set it again after you've added the items.
    Basically like this (from your source code):
                this.DataContext = null;  
                Items.Clear();  
                for (var itemIndex = 0; itemIndex < 5; itemIndex++)  
                {  
                    Items.Add(new Test()  
                    {  
                        Title = "Item " + itemIndex,  
                        Content = "Load #" + _loadCount + " || Item #" + itemIndex  
                    });  
                }  
                this.DataContext = this;  
     

    However, I'm not sure this is the recommended use of the control and also I would suggest that you look into the use of templates and databinding.
    As a quick example:
    <controls:Pivot x:Name="MyPivot" Title="MY APPLICATION" ItemsSource="{Binding Items}">  
                <controls:Pivot.HeaderTemplate> 
                    <DataTemplate> 
                        <TextBlock Text="{Binding Title}"/>  
                    </DataTemplate> 
                </controls:Pivot.HeaderTemplate> 
                <controls:Pivot.ItemTemplate> 
                    <DataTemplate> 
                        <TextBlock Text="{Binding Content}"/>  
                    </DataTemplate> 
                </controls:Pivot.ItemTemplate> 
            </controls:Pivot> 

    And then use a ObservableCollection<MyItem> instead of PivotItem (it gets automatically wrapped)
    public class MyItem{  
            public string Title { getset; }  
            public string Content { getset; }  
        } 
  • 11/2/2010 4:18 PM In reply to

    Re: Pivot control Items property manipulation

    I was quite hopeful with the "MyItem" approach to solving the problem, but unfortunatly it dind't solve it either (at least on my tests!)... even with a bindable SelectedIndex... :(
  • 11/2/2010 6:29 PM In reply to

    Re: Pivot control Items property manipulation

    Actually, the "MyItem" approach was more of a general advice, it doesn't solve your issue.
    As I said, if you clear out the DataContext before you alter the ObservableCollection and then sets the DataContext again afterwards, it will work.

    The issue you're having is this:
    - Select item 4
       - This sets the internal header collection to selected header item 4
    - Clear out the ObservableCollection
       - All items seems to disappear, but the internal header collection still remembers its current index
    - Add items to the ObservableCollection
       - For every item added it triggers an layout update.
       - It then tries to redraw the headers, still remembering its old index
       - Fails because the item at the index position doesn't exist anymore

    At least thats what I was seeing as I did a quick debug of your application.
    By setting the DataContext to null the automatic update will not occur when you add items to the ObservableCollection and when you then set the DataContext again, the pivot control will rebuild its internal header collection and such - basically resetting the control.

  • 11/2/2010 6:33 PM In reply to

    Re: Pivot control Items property manipulation

    You're totally right, Frode! :)

    It's quite a *hack*, but it seems to be the only way to solve this problem... I do hope someone in Microsoft solves this FAST, because this is a huge bug!!!!!
Page 1 of 1 (7 posts) Previous Discussion Next Discussion