0

对于我的语义缩放控制,我试图让不包含孩子的字母出现,但我无法像我目前拥有的代码一样实现这一点,只有包含孩子的字母出现。努力弄清楚我需要修改或添加哪一部分代码才能实现我想要的。有任何想法吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EELL
{
    using System;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Media;

#if DISABLE_SAMPLE_DATA
    internal class SampleDataSource { }
#else

    public class Item : System.ComponentModel.INotifyPropertyChanged
    {
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        private string _Station = string.Empty;
        public string Station
        {
            get
            {
                return this._Station;
            }

            set
            {
                if (this._Station != value)
                {
                    this._Station = value;
                    this.OnPropertyChanged("Station");
                }
            }
        }

        private string _Zone = string.Empty;
        public string Zone
        {
            get
            {
                return this._Zone;
            }

            set
            {
                if (this._Zone != value)
                {
                    this._Zone = value;
                    this.OnPropertyChanged("Zone");
                }
            }
        }

        private string _Link = string.Empty;
        public string Link
        {
            get
            {
                return this._Link;
            }

            set
            {
                if (this._Link != value)
                {
                    this._Link = value;
                    this.OnPropertyChanged("Link");
                }
            }
        }
    }

    public class GroupInfoList<T> : List<object>
    {

        public object Key { get; set; }


        public new IEnumerator<object> GetEnumerator()
        {
            return (System.Collections.Generic.IEnumerator<object>)base.GetEnumerator();
        }
    }


    public class StoreData
    {
        public StoreData()
        {
            Item item;

            item = new Item();
            item.Station = "Aldgate";
            item.Link = "/Lines and Stations/Metropolitan/Aldgate_(Metropolitan).xaml";
            Collection.Add(item);


            item = new Item();
            item.Station = "Moorgate";
            item.Link = "/Lines and Stations/Metropolitan/MOG_(Metropolitan).xaml";
            Collection.Add(item);
        }



        private ItemCollection _Collection = new ItemCollection();

        public ItemCollection Collection
        {
            get
            {
                return this._Collection;
            }
        }

        internal List<GroupInfoList<object>> GetGroupsByCategory()
        {
            List<GroupInfoList<object>> groups = new List<GroupInfoList<object>>();

            var query = from item in Collection
                        orderby ((Item)item).Zone
                        group item by ((Item)item).Zone into g
                        select new { GroupName = g.Key, Items = g };
            foreach (var g in query)
            {
                GroupInfoList<object> info = new GroupInfoList<object>();
                info.Key = g.GroupName;
                foreach (var item in g.Items)
                {
                    info.Add(item);
                }
                groups.Add(info);
            }

            return groups;
        }

        internal List<GroupInfoList<object>> GetGroupsByLetter()
        {
            List<GroupInfoList<object>> groups = new List<GroupInfoList<object>>();

            var query = from item in Collection
                        orderby ((Item)item).Station
                        group item by ((Item)item).Station[0] into g
                        select new { GroupName = g.Key, Items = g };
            foreach (var g in query)
            {
                GroupInfoList<object> info = new GroupInfoList<object>();
                info.Key = g.GroupName;
                foreach (var item in g.Items)
                {
                    info.Add(item);
                }
                groups.Add(info);
            }

            return groups;

        }
    }

    public class ItemCollection : IEnumerable<Object>
    {
        private System.Collections.ObjectModel.ObservableCollection<Item> itemCollection = new System.Collections.ObjectModel.ObservableCollection<Item>();

        public IEnumerator<Object> GetEnumerator()
        {
            return itemCollection.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(Item item)
        {
            itemCollection.Add(item);
        }
    }
#endif
}

错误 在此处输入图像描述

4

1 回答 1

2

更新代码:

internal List<GroupInfoList<object>> GetGroupsByLetter()
    {
      var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToList();
        var groupByAlphabets = from letter in letters
                   select new
                   {
                       Key = letter,
                       Items = (from item in Collection
                                where ((Item)item).Station.StartsWith(letter.ToString(), StringComparison.CurrentCultureIgnoreCase)
                                orderby ((Item)item).Station
                                group item by ((Item)item).Station[0] into g
                                select g)
                   };

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

        foreach (var g in groupByAlphabets)
        {
            GroupInfoList<object> info = new GroupInfoList<object>();
            info.Key = g.Key;
            foreach (var item in g.Items)
            {
                info.Add(item);
            }
            groups.Add(info);
        }

        return groups;
    }

// 上面的代码应该按字母给你分组

<Style x:Key="mainGridViewStyle" TargetType="GridView">
<Setter Property="GroupStyleSelector" Value="{StaticResource GridViewGroupStyleSelector}" />

<views:GridViewGroupStyleSelector x:Key="GridViewGroupStyleSelector" />

<GroupStyle x:Key="gridViewGroupStyle" HidesIfEmpty="True">
    <GroupStyle.HeaderTemplate>
        <DataTemplate>
            <Grid x:Name="headerGrid">
                <StackPanel Orientation="Horizontal" Margin="-7,0,0,-7">
                  <TextBlock Style="{StaticResource SubheaderTextBlockStyle}" Text="{Binding HeaderIdentifier}" />
                    </Button>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </GroupStyle.HeaderTemplate>
</GroupStyle>

  public class GridViewGroupStyleSelector : GroupStyleSelector
{
    protected override GroupStyle SelectGroupStyleCore(object group, uint level)
    {
        return (GroupStyle)App.Current.Resources["gridViewGroupStyle"];
    }
}

所以我们在这里:

  • mainGridView 是我的主要网格视图。
  • 它定义了一个名为 mainGridViewStyle 的样式,我们在其中设置的属性之一是 GroupStyleSelector
  • GroupStyleSelector 被定义为 GridViewGroupStyleSelector 的静态资源。
  • 实际实现在代码文件中作为类 GridViewGroupStyleSelector 完成
  • 这个选择器提供了实际的组样式,称为 gridViewGroupStyle,在这个样式中我们标记属性 HidesIfEmpty 是 True 还是 false。
于 2015-02-25T17:34:15.853 回答