代码之家  ›  专栏  ›  技术社区  ›  dkackman Srinivas Kokkula

数据绑定数据透视控件未创建第一个数据透视项

  •  13
  • dkackman Srinivas Kokkula  · 技术社区  · 14 年前

    <controls:Pivot x:Name="Pivoter" Title="{Binding Name}" 
          TitleTemplate="{StaticResource PivotTitleTemplate}" 
          HeaderTemplate="{StaticResource PivotHeaderTemplate}"
          ItemsSource="{Binding Items}"
          ItemTemplate="{StaticResource DisplayItemDataTemplate}">
    </controls:Pivot >
    

    使用此数据模板:

    <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

    4 回复  |  直到 14 年前
        1
  •  4
  •   Jab    14 年前

    我遇到了同样的问题。

    似乎应该在构造函数中设置数据透视的DataContext。我在Page_Loaded事件中设置了DataContext,如前所述,第一个pivot项不会触发loadeng事件。通过简单地在前面绑定我的数据上下文,事件开始触发。

        2
  •  3
  •   Julian    14 年前

    我运行了这个程序,但是我无法通过在构造函数中设置数据上下文来解决这个问题,因为要显示的数据取决于来自NavigationService的信息,所以我必须在页面加载事件处理程序中加载数据。

    示例代码:

    void ChannelOverview_Loaded(object sender, RoutedEventArgs e)
        {
            string channelSystemName;
            if(this.NavigationContext.QueryString.TryGetValue("channelSystemName", out channelSystemName))
            {
                this.viewModel.LoadData(this.Dispatcher, channelSystemName);
                //Set the SelectedIndex to one,
                //otherwise the pivot-view won't render the first item.
                this.overviewPivot.SelectedIndex = 1;
            }
            else 
            {
                MessageBox.Show("Kanalen hade ett felaktigt Id ");
            }
        }
    
        3
  •  1
  •   sraj    12 年前

    pivotTypes.SelectedItem = pivotTypes.Items[ 0 ];
    
        4
  •  0
  •   Emond    13 年前

    This blogpost 解决了我的问题。

    我对其他提供的解决方案的问题是,我无法使用构造函数设置数据上下文,有时,只有一个数据透视项,因此我可以来回切换以加载内容。

    解决方案非常简单:在更新绑定轴心点以将其DataContext设置为null的集合之前。然后更新集合,然后将DataContext设置为集合。