【问题标题】:GridView displays wrong data for last items WinRTGridView 显示最后一项 WinRT 的错误数据
【发布时间】:2015-05-04 06:08:30
【问题描述】:

我遇到了一个奇怪的问题,我绑定到 CollectionViewSource 的 GridView 显示最后一项的错误数据。但实际绑定是正确的。如果我监听 ItemClicked,DataContext 是正确的,UI 只是在 GridView 中显示上一个项目的信息。我的目标是绑定到 CollectionViewSource 中的一组分组数据,但如果我只是直接绑定到 ObservableCollection,我什至会发生这种情况。

这是我的 XAML:

<Page.Resources>
<CollectionViewSource x:Name="CuratedCVS" IsSourceGrouped="True"  />
</Page.Resources>

<SemanticZoom Padding="0">
<SemanticZoom.ZoomedInView>
    <GridView x:Name="CuratedFeedsList" 
              ItemsSource="{Binding Source={StaticResource CuratedCVS}}"
              ItemTemplate="{StaticResource CuratedFeedItemTemplate}"
              SelectedItem="{Binding SelectedFeed, Mode=TwoWay}"
              SelectionMode="Single"
              IsSwipeEnabled="True"
              IsSynchronizedWithCurrentItem="False"
              ShowsScrollingPlaceholders="True"
              Padding="20,0">
        <GridView.GroupStyle>
            <GroupStyle HeaderTemplate="{StaticResource CuratedFeedHeaderTemplate}"/>
        </GridView.GroupStyle>
    </GridView>
</SemanticZoom.ZoomedInView>
<SemanticZoom.ZoomedOutView>
    <ListView Background="{Binding SecondaryBackgroundBrush, Source={StaticResource ThemeManager}}" 
              ItemsSource="{Binding Source={StaticResource CuratedCVS}, Path=CollectionGroups}"
              ItemTemplate="{StaticResource CuratedFeedJumpTemplate}"
              Padding="0,10">
    </ListView>
</SemanticZoom.ZoomedOutView>
</SemanticZoom>

这是我对数据进行分组的代码(似乎可行):

var result =
        from f in CuratedFeeds
        group f by f.Category into g
        orderby g.Key
        select g;
    CuratedCVS.Source = result;

(其中 CuratedFeeds 是 ObservableCollection)

有人见过这个吗?这只发生在 Windows 8.1 应用程序中。它可以在 Windows Phone 8.1 应用程序中完美运行。

【问题讨论】:

    标签: c# gridview windows-runtime windows-8.1


    【解决方案1】:

    所以我发现了问题所在,不幸的是它与我发布的代码没有任何关系。我正在自定义 ItemContainerStyle,我在其中使用了这个:

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Margin" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <ListViewItemPresenter Content="{Binding}" Margin="0"
                                               SelectedBackground="{Binding SecondaryBackgroundBrush, Source={StaticResource ThemeManager}}"
                                               SelectedForeground="{Binding ForegroundBrush, Source={StaticResource ThemeManager}}"
                                               PlaceholderBackground="{Binding BackgroundBrush, Source={StaticResource ThemeManager}}"
                                               PointerOverBackground="{Binding SecondaryBackgroundBrush, Source={StaticResource ThemeManager}}"
                                               SelectionCheckMarkVisualEnabled="False"
                                               Padding="0,0,0,0"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
    

    在此代码中,问题在于 Content="{Binding}"。我不知道为什么,但这搞砸了数据。我所要做的就是删除 Content="{Binding}" 并且效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多