【问题标题】:XAML grouped GridView/Semantic Zoom not displaying all children?XAML 分组 GridView/语义缩放不显示所有子项?
【发布时间】:2012-10-10 23:40:12
【问题描述】:

我正在尝试使用 XAML C# Grouped GridView sample 使我的 SemanticZoom 在 XAML C# Windows 8 应用程序中工作。问题是,由于某种原因,它显示了正确的标题(在这种情况下是类别),但它没有显示标题下的所有项目(当我在其中一些项目中有多达 6 个项目时,它只显示每个项目)。

这是 SemanticZoom 的 XAML 代码(请注意,为简洁起见,我省略了 ZoomedOutView,因为它运行良好):

<SemanticZoom x:Name="boardZoom" Height="626" Margin="10,132,10,0" VerticalAlignment="Top">
    <SemanticZoom.ZoomedInView>
        <GridView IsSwipeEnabled="True" x:Name="ItemsGridView" Tapped="Tapped" ItemsSource="{Binding Source={StaticResource cvs2}}" SelectionChanged="selChanged">
            <GridView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="10,10,0,0" 
                    HorizontalAlignment="Left" VerticalAlignment="Stretch">
                        <Image Source="{Binding Thumbnail}"></Image>                                
                       <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Width="500"
                            FontFamily="Global User Interface" FontSize="40" VerticalAlignment="Top" />                                
                        <TextBlock Text="{Binding pinNumber}" x:Name="PinNum" Visibility="Collapsed"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </GridView.ItemTemplate>         
            <GridView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                           <TextBlock Text='{Binding Key}' Foreground="White" Margin="5" FontSize="20" FontFamily="Segoe UI Light" />
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="GroupItem">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="GroupItem">
                                        <StackPanel Orientation="Vertical">
                                            <ContentPresenter Content="{TemplateBinding Content}" />
                                            <ItemsControl x:Name="ItemsControl" ItemsSource="{Binding GroupItems}" />
                                        </StackPanel>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <VariableSizedWrapGrid Orientation="Vertical" MaximumRowsOrColumns="5" />
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </GridView.GroupStyle>
            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapGrid Orientation="Vertical" MaximumRowsOrColumns="1" />
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
        </GridView>
    </SemanticZoom.ZoomedInView>

以及应用启动时调用的 Refresh() C# 函数:

System.Collections.ObjectModel.ObservableCollection<SemanticZoomed> finalSource = new System.Collections.ObjectModel.ObservableCollection<SemanticZoomed>();
public async Task<bool> Refresh()
{
    var Pins = await pinTable.ReadAsync(); //pinTable is an Azure Mobile Services table
    List<string> categoriesMixed = new List<string>();
    if (Pins.ToArray().Length < 1)
    {
        //adds a new "Welcome" pin to the table, taken out for brevity
    }
    foreach (pin nowPin in Pins)
    {
        SemanticZoomed toAdd = new SemanticZoomed();
        toAdd.Category = nowPin.category;
        toAdd.pinNumber = nowPin.Id.ToString();
        toAdd.Title = nowPin.name;
        categoriesMixed.Add(nowPin.category);
        finalSource.Add(toAdd);
    }

    List<GroupPinList<object>> groups = new List<GroupPinList<object>>();

    var query = from nowPin in finalSource
        orderby ((SemanticZoomed)nowPin).Category
                group nowPin by ((SemanticZoomed)nowPin).Category into g
                select new { GroupName = g.Key, Items = g };
    foreach (var g in query)
    {
        GroupPinList<object> info = new GroupPinList<object>();
        info.Key = g.GroupName;
        foreach (var item in g.Items)
        {
           info.Add(item);
        }
        groups.Add(info);
    }
    cvs2.Source = groups;
    (boardZoom.ZoomedOutView as ListViewBase).ItemsSource = cvs2.View.CollectionGroups;
    return true;
}

这里有一些关于组变量的截图,以及生成的 SemanticZoom 显示的内容:

组变量:

groups 变量中的“欢迎”类别(它正确显示了它的 6 个项目,以及 imgSrc 旁边的错误“无法获取字段 'imgSrc' 的值,因为有关包含类的信息不可用”,该错误在cvs2 下面):

cvs2(它显示了欢迎类别下的 6 个不同的项目):

最后,它最终显示的内容:

我不知道欢迎类别中的其他别针去了哪里。 XAML 代码中是否有我遗漏的内容?任何帮助将不胜感激:)

【问题讨论】:

    标签: c# xaml windows-8 windows-runtime


    【解决方案1】:

    我也有同样的问题。这解决了 pb 。

    在 SemanticZoom.ZoomedInView 中替换

    <ItemsPanelTemplate>
    <WrapGrid Orientation="Vertical" MaximumRowsOrColumns="1" />
    </ItemsPanelTemplate>
    

    通过

    <ItemsPanelTemplate>
             <VirtualizingStackPanel Orientation="Vertical"/>
    </ItemsPanelTemplate>
    

    【讨论】:

      【解决方案2】:

      我想我知道问题出在哪里 - 如果您以编程方式一个接一个地向 GridView 添加项目,就会发生这种情况。这里发生的情况是,当您将第一个包含 n 个项目的组添加到 GridView 源时,然后保留编号 n,并且对于之后添加的每个组,它显示的项目不超过 n 个,即使有更多项目也是如此。

      因此,如果您有 5 个包含 2、4、1、5、3 项的集合,则将空 ObservableCollection 分配为 GridView 的 ItemSource,然后将这些组添加到 ObservableCollection 中,您只会看到 2每组有 ,2,1,2,2 项。

      我不知道为什么会发生这种情况,如果它是功能或错误,但您可以通过先加载 ObservableCollection 然后将其分配给 GridView 作为源来解决它,或者您可以使用某种转换器,这将为每个组返回相同数量的项目。对于数量较少的组,您可以添加假的空项目并使用不同的 DataTemplate 隐藏它们。

      编辑:我刚刚注意到您已经在一次添加集合,所以问题可能出在其他地方。也许您的问题的根源是 ItemsPanel 中的这个?

      MaximumRowsOrColumns="1"
      

      【讨论】:

      • 不幸的是,这似乎不是问题所在。例如,将 MaximumRowsOrColumns="1" 更改为 MaximumRowsOrColumns="5" 结果 in this
      • 啊,谢谢,看起来您的解决方案是“或者您可以使用某种转换器,这将为每个组返回相同数量的项目。”修复它;D
      • 嗨,您可以在此处为其他人发布转换器/解决方案吗?
      • 我看看我是否还需要投影,希望很快我可以为你复制/粘贴。
      【解决方案3】:
      you need to use stackpanel instead of  WrapGrip in ItemPanelTemplate
      
      <GridView.ItemsPanel>
                 <ItemsPanelTemplate>
                     <StackPanel Orientation="Horizontal" />
                 </ItemsPanelTemplate>
      </GridView.ItemsPanel>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-11
        • 1970-01-01
        • 2012-05-10
        • 1970-01-01
        • 2013-01-19
        相关资源
        最近更新 更多