语义缩放 - 不包含没有孩子出现的字母

时间:2015-02-18 18:16:35

标签: c# winrt-xaml semantic-zoom

对于我的语义缩放控制,我试图让不包含子项的字母出现,但是我遇到的问题与我目前的代码一样,只有包含子项的字母出现。努力弄清楚我需要修改或添加哪部分代码以实现我想要的。有什么想法吗?

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
}

错误 enter image description here

1 个答案:

答案 0 :(得分: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是我的主要gridview。
  • 它为它定义了一个名为mainGridViewStyle的样式,我们在其中设置的属性之一是GroupStyleSelector
  • GroupStyleSelector被定义为GridViewGroupStyleSelector的静态资源。
  • 实际的实现在代码文件中作为类GridViewGroupStyleSelector
  • 完成
  • 此选择器提供实际的组样式,称为gridViewGroupStyle,在此样式中,我们将属性HidesIfEmpty标记为True或false。