在 WP7 上将 PivotItem 数据绑定到 ObservableCollection

发布于 2024-10-15 19:38:22 字数 1104 浏览 2 评论 0原文

我想将 ObservableCollection 数据绑定到 WP7 中的 Pivot 控件,以便 ObservableCollection 中的每个对象都成为 PivotItem。这是我使用的代码:

    <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>

这适用于我的 ObservableCollection 中的 Tre 项目,我得到了三个 PivotItems。但是,当所有内容都加载后,DataTemplate 内的绑定将不会更新。只有当我滚动到下一个 PivotItem 时,FirstName、LastName 和 HomeTown 才会加载。

这是为什么?我缺少什么?

谢谢

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 inside 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蛮可爱 2024-10-22 19:38:22

我遇到了同样的问题,但设置 SelectedIndex=1 的解决方法不适合我。

我找到了另一个解决方案:当您将 Item 添加到 Persons 集合时,您应该首先创建一个临时元素,并且仅当您填充所有数据时才将其添加到您的 Persons 集合中。

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

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);
若言繁花未落 2024-10-22 19:38:22

做了简单的测试后,我无法重现这种行为。我在相当于 FirstName 的 get 块中放置了一个断点,在 ObservableCollection 中包含两个项目,我得到了两次命中。

您如何检测到它未绑定?您看不到“下一个”pivotitems 内容,那怎么办呢?

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?

第七度阳光i 2024-10-22 19:38:22

听起来加载顺序或通知代码有问题。

检查在设置每个 FirstName、LastName 和 HomeTown 成员的属性时是否正确触发 PropertyChanged 事件。

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.

纵情客 2024-10-22 19:38:22

我是这样做的。对我来说,问题是集合会异步更新以响应 Web 方法调用。

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);
    }
}

关键是连接枢轴项所绑定到的集合的观察者,并在数据上下文更新时对其进行震动。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文