DataBound Pivot 控件未创建第一个 PivotItem
在 Windows Phone 7 页面中,我有以下控件:
<controls:Pivot x:Name="Pivoter" Title="{Binding Name}"
TitleTemplate="{StaticResource PivotTitleTemplate}"
HeaderTemplate="{StaticResource PivotHeaderTemplate}"
ItemsSource="{Binding Items}"
ItemTemplate="{StaticResource DisplayItemDataTemplate}">
</controls:Pivot >
使用此 DataTemplate:
<DataTemplate x:Key="DisplayItemDataTemplate">
<Image Grid.Column="0" Stretch="Uniform"
Source="{Binding LargeImage, Converter={StaticResource UriBitmapConverter}}"/>
<StackPanel Grid.Column="1" Orientation="Vertical">
<HyperlinkButton NavigateUri="{Binding Uri}" Content="{Binding Uri}"/>
</StackPanel>
</DataTemplate>
ItemsSource
是一个 ObservableCollection
。显示页面时,它会创建所有 PivotItems,但不会创建第一个项目,除非我向前和向后滚动到它。它在滚动列表中有一个条目,但没有 PivotItem
控件。
如果我在枢轴控件的 LoadingPivotItem 事件中放置一个断点,则在首次显示枢轴时不会调用该断点,但只有当我滚动并返回到第一个项目时才会再次触发该断点。
有人见过 Pivot 控件的类似行为并有解决方法吗?或者我做错了什么?
In a Windows Phone 7 page I have the following control:
<controls:Pivot x:Name="Pivoter" Title="{Binding Name}"
TitleTemplate="{StaticResource PivotTitleTemplate}"
HeaderTemplate="{StaticResource PivotHeaderTemplate}"
ItemsSource="{Binding Items}"
ItemTemplate="{StaticResource DisplayItemDataTemplate}">
</controls:Pivot >
with this DataTemplate:
<DataTemplate x:Key="DisplayItemDataTemplate">
<Image Grid.Column="0" Stretch="Uniform"
Source="{Binding LargeImage, Converter={StaticResource UriBitmapConverter}}"/>
<StackPanel Grid.Column="1" Orientation="Vertical">
<HyperlinkButton NavigateUri="{Binding Uri}" Content="{Binding Uri}"/>
</StackPanel>
</DataTemplate>
The ItemsSource
is an ObservableCollection
. When the page is displayed it creates all of the PivotItems
but the first item does not get created unless I scroll forward and back to it. It has an entry in the scroll list but no PivotItem
control.
If I put a break point in the Pivot Control's LoadingPivotItem
event it is not called when the pivot is first displayed but again only gets hit when I scroll away and back to the first item.
Has anybody seen similar behavior for the Pivot
control and have a work around? Or am I doing something incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题。
看来应该在构造函数中设置数据透视表的 DataContext。我在 Page_Loaded 事件中设置 DataContext,第一个数据透视项不会触发 Loading 事件,如所述。通过简单地绑定我的 DataContext ,事件就开始触发。
我相信这是框架中的一个错误,但我还没有做足够的工作来确认它。
I ran into this same problem.
It appears the DataContext for the pivot should be set in the constructor. I was setting my DataContext in the Page_Loaded event and the first pivot item would not fire the Loading event, as described. By simply binding my DataContext earlier, the event started firing.
I believe it's a bug in the framework, but I haven't done enough to confirm it.
我遇到了这个问题,我无法通过在构造函数中设置数据上下文来解决这个问题,因为要显示的数据取决于来自 NavigationService 的信息,所以我必须在 page_loaded- 中加载数据事件处理程序。
但是通过在页面加载事件中将枢轴控件 SelectedIndex 设置为 1,它神奇地开始工作。这不是一个理想的解决方案,但在这种情况下,它是一个我必须修复的障碍。
示例代码:
I ran in to this, and I couldn't solve this by setting the data-context in the constructor, as the data that was going to displayed was depending on information from the NavigationService, so I had to load the data in the page_loaded-event handler.
But by setting the pivot-controls SelectedIndex to 1 in the page-loaded-event, it magically started working. Its not an ideal solution, but in this case, it was a blocker that I just had to fix.
Sample code:
可能有点晚了,但可能会对某人有所帮助。如文档所述,如果数据透视表的 SelectedIndex 为 0 并且数据上下文更改导致 SelectedIndex 再次为 0,则不会更新第一个数据透视表项,也不会调用数据透视表项加载事件。但是,当我有数据绑定枢轴控件时,以下操作也有效。请注意,pivotTypes.Items[ 0 ] 返回数据绑定对象而不是数据透视项。
Might be a bit late, but will might help somebody. As documented, SelectedIndex of the pivot if 0 and a data context change resulting in SelectedIndex being 0 again does not update the first pivot item and neither calls pivot item loading event. However, following also worked when i had a data bound pivot control. Note that pivotTypes.Items[ 0 ] returns the data bound object and not the pivot item.
这篇博文解决了我的问题。
我对其他提供的解决方案的问题是,我无法使用构造函数来设置 DataContext,并且有时只有一个 PivotItem,因此我可以来回切换以加载内容。
解决方案非常简单:在更新集合之前,Pivot 必须将其 DataContext 设置为 null。然后更新集合,然后将 DataContext 设置为集合。
This blogpost solved my problem.
My issue with the other offered solutions was that I wasn't able to use the constructor to set the DataContext and that, at times, there was just one PivotItem so I could switch back and forth to load the content.
The solution is extremely simple: before updating the collection that the Pivot is bound to set its DataContext to null. Then update the collection and after that set the DataContext to the collection.