在 WP7 上将 PivotItem 数据绑定到 ObservableCollection
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
检查这个讨论:
DataBound Pivot 控件未创建第一个 PivotItem
Check this discussion:
DataBound Pivot control is not creating the first PivotItem
我遇到了同样的问题,但设置 SelectedIndex=1 的解决方法不适合我。
我找到了另一个解决方案:当您将 Item 添加到 Persons 集合时,您应该首先创建一个临时元素,并且仅当您填充所有数据时才将其添加到您的 Persons 集合中。
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.
做了简单的测试后,我无法重现这种行为。我在相当于 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?
听起来加载顺序或通知代码有问题。
检查在设置每个 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.
我是这样做的。对我来说,问题是集合会异步更新以响应 Web 方法调用。
关键是连接枢轴项所绑定到的集合的观察者,并在数据上下文更新时对其进行震动。
Here's how I do it. The problem for me is that the collection updates asynchronously in response to a web method call.
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.