开发者

Databinding PivotItems to ObservableCollection on WP7

开发者 https://www.devze.com 2023-02-08 01:49 出处:网络
I want to databind an ObservableCollection to a Pivot contronl in WP7 so that each object in my ObservableCollection becomes a PivotItem. This is the code I use:

I want to databind an ObservableCollection to a Pivot contronl in WP7 so that each object in my ObservableCollection becomes a PivotItem. This is the code I use:

    <controls:Pivot x:Name="MainPivot" ItemsSource="{Binding Persons}">
        <controls:Pivot.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding FullName}"/>
            </DataTemplate>
        </controls:Pivot.HeaderTemplate>
        <controls:Pivot.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="0,0,0,17" Width="432">
                    <TextBlock Text="{Binding FirstName}"/>
                    <TextBlock Text="{Binding LastName}"/>
                    <TextBlock Text="{Binding HomeTown}"/>
                </StackPanel>
            </DataTemplate>
        </controls:Pivot.ItemTemplate>
    </controls:Pivot>

This works and with tre items in my ObservableCollection I get three PivotItems. But when everything gets loaded the binding i开发者_开发技巧nside the DataTemplate won´t get updated. It is only when I scroll to the next PivotItem that the FirstName, LastName and HomeTown gets loaded.

Why is that? What am I missing?

Thanks


Check this discussion: DataBound Pivot control is not creating the first PivotItem


I had the same problem, but the workaround with setting SelectedIndex=1 didn't suit me.

I found the other solution: when you adding the Item to your Persons collection you should first create a temp element and only when you fill all data add it to your Persons collection.

Person tempPers = new Person() { FullName = "Abduvaliev Edem", FirstName = "Edem", LastName = "Abduvaliev", HomeTown = "Sevastopol"};
Pesrons.Add(tempPers);


After doing a simple test i cannot reproduce this behavior. I i put a breakpoint inside the get block of the equivalent of FirstName with two items in my ObservableCollection i get two hits.

How did you detect that it is not bound? You cannot see the "next" pivotitems content, so how?


Sounds like there's some problem with the loading order - or with the notification code.

Check that you are correctly firing the PropertyChanged event when you set the properties on each of your FirstName, LastName and HomeTown members.


Here's how I do it. The problem for me is that the collection updates asynchronously in response to a web method call.

void Page_Loaded(object sender, RoutedEventArgs e)
{
    _log.Debug("Page loaded.");

    var vm = this.GetViewModel<TrendsViewModel>();

    if (!vm.IsInitialized)
    {
        vm.PivotItems.CollectionChanged += (origin, args) =>
        {
            this.PivotControl.DataContext = null;
            this.PivotControl.DataContext = vm;
        };

        vm.Initialize(this);
    }
}

The key is hooking up an observer of the collection to which the Pivot items are bound and giving the data context a shake when it updates.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号