在网格视图上对项目进行分组

时间:2012-12-11 06:39:33

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

我有警报模型对象,其中包含重复集合,其中包含警报应重复的日期。我想在星期几(如星期一,星期二等)分组的网格视图中显示警报。

我将所有这些警报添加到“警报”

集合中

对于警报中的每个警报,我再次在警报的重复收集中为每一天创建警报,并将它们全部添加到集合“TotalAlarms”。

foreach (Alarm alarm in this.Config.Alarms)
        {
            foreach (DayOfWeek day in alarm.Repeat)
            {
                this.tempAlarm = this.CopyAlarm(alarm);

                tempAlarm.DayOfWeek = day;                 

                TotalAlarms.Add(tempAlarm);
            }
        }

我正在使用linq对警报模型的DayOfWeek属性进行分组,该属性指示警报应该关闭的日期。

var result = from t in _ViewModel.TotalAlarms
                     group t by  t.DayOfWeek into q                        
                     orderby q.Key
                     select q;

我将此结果添加到groupedItemsViewSource(绑定到网格视图的itemsource)

groupedItemsViewSource.Source = result;

并且对于网格视图的标题,我将它绑定到“Key”

<TextBlock Text="{Binding Key}" Style="{StaticResource TitleTextStyle}" FontSize="20" HorizontalAlignment="Stretch" VerticalAlignment="Center" Width="100" Height="30" Margin="5"/>

此方法仅显示发生警报的星期几。就像警报设置为星期五和星期六一样,只有星期五和星期六会显示在组标题中。

我想要的是所有日子都显示为组标题,如果当天没有警报,则它可以为空。但是组标题应该显示所有日期。

我真的很难想到办法做到这一点。如果有人有任何想法,请帮助我....

由于

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我终于找到了自己的答案。它有点凌乱和丑陋,但它的工作原理。我现在使用7个网格视图而不是一个。但除非网格视图中有数据项,否则它仍然不会显示标题。因此,如果网格视图集合为空,那么我将一些空对象添加到绑定网格视图的集合中,然后我减小网格视图的高度,以便不显示空项目,只显示标题。

这是一个网格视图的示例代码。

 emptyAlarmsList = new ObservableCollection<Alarm>();
        emptyAlarmsList.Add(new Alarm());

        var sundayAlarms = from t in _ViewModel.TotalAlarms
                           where t.DayOfWeek == DayOfWeek.Sunday
                           group t by t.DayOfWeek into g
                           orderby g.Key
                           select g;

        if (sundayAlarms.Count() == 0)
        {
            var sundayEmptyAlarms = from t in this.emptyAlarmsList                                   
                               group t by t.DayOfWeek into g
                               orderby g.Key
                               select g;
            SundayAlarmsView.Source = sundayEmptyAlarms;
            itemGridView1.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
            itemGridView1.Height = 100;
        }
        else
            SundayAlarmsView.Source = sundayAlarms;

一个网格视图的XAMl代码

 <CollectionViewSource
        x:Name="SundayAlarmsView"            
        IsSourceGrouped="true"
        />
            <GridView
        x:Name="itemGridView1"
        AutomationProperties.AutomationId="ItemGridView1"
        AutomationProperties.Name="Grouped Items"            
        Grid.Column="0"
        Margin="0,-3,0,0"
        Padding="116,0,40,46"
        SelectionMode= "Extended"
        ItemsSource="{Binding Source={StaticResource SundayAlarmsView}}"
        ItemTemplate="{StaticResource AlarmListTemplate}"
        SelectionChanged="Alarm_SelectionChanged">

                <GridView.ItemContainerStyle>
                    <Style
                TargetType="GridViewItem">
                        <Setter
                    Property="Height"
                    Value="150" />
                        <Setter
                    Property="Width"
                    Value="250" />
                        <Setter
                    Property="Margin"
                    Value="10,10" />
                    </Style>
                </GridView.ItemContainerStyle>

                <GridView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </GridView.ItemsPanel>
                <GridView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>

                                <TextBlock Text="Sunday" Style="{StaticResource TitleTextStyle}" FontSize="20" HorizontalAlignment="Stretch" VerticalAlignment="Center" Width="100" Height="30" Margin="5"/>

                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                    </GroupStyle>
                </GridView.GroupStyle>
            </GridView>

答案 2 :(得分:0)

我将这个用于我的项目

注意:我想按表格中的一个字段进行分组

我的模特是:

 public class Item : INotifyPropertyChanged
    {
        public Item()
        {
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private string _name;
        public string name { set { _name = value; OnPropertyChanged("name"); } get { return _name; } }
        private string _color;
        public string color { set { _color = value; OnPropertyChanged("color"); } get { return _color; } }
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

    }
xaml gridview中的

是:

<GridView x:Name="gr" SelectionChanged="gr_SelectionChanged"  ItemsSource="{Binding Source={StaticResource CollectionViewSource}}" Margin="-400,30,0,0" SelectionMode="Multiple" SelectedValuePath="{Binding selectedItemFalg, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                <GridView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <ItemsWrapGrid Orientation="Horizontal" Width="500" />
                    </ItemsPanelTemplate>
                </GridView.ItemsPanel>
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"  Background="#FFD7EDF2" Margin="0,0,0,0" Width="80">
                            <TextBlock Text="{Binding name}" Foreground="#FF00455A"  Margin="5,5,0,0"  Height="30" />
                            <TextBlock Text="-" Foreground="#FF00455A"  Margin="5,5,0,0"  Height="30" />
                            <TextBlock Text="{Binding color}" Foreground="#FF00455A"  Margin="5,5,0,0"  Height="30" />
                        </StackPanel>
                    </DataTemplate>
                </GridView.ItemTemplate>
                <GridView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <Grid Background="Blue" Margin="10">
                                    <TextBlock Text='{Binding Key}' Foreground="Black" FontSize="25" Margin="5" Width="80"/>
                                </Grid>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                    </GroupStyle>
                </GridView.GroupStyle>
            </GridView>

在viewModel中需要以下代码:

 public  class date_for_my_page
    {
       public date_for_my_page()
       {
           Item item = new Item();
           item.color = "black1";
           item.name = "A";
           Collection.Add(item);
           item = new Item();
           item.color = "black2";
           item.name = "A";
           Collection.Add(item);
           item = new Item();
           item.color = "black3";
           item.name = "A";
           Collection.Add(item);
           item = new Item();
           item.color = "black4";
           item.name = "A";
           Collection.Add(item);
           item = new Item();
           item.color = "black5";
           item.name = "A";
           Collection.Add(item);
           item = new Item();
           item.color = "blue1";
           item.name = "B";
           Collection.Add(item);
           item = new Item();
           item.color = "blue2";
           item.name = "B";
           Collection.Add(item);
           item = new Item();
           item.color = "blue3";
           item.name = "B";
           Collection.Add(item);
           item = new Item();
           item.color = "Red1";
           item.name = "C";
           Collection.Add(item);
           item = new Item();
           item.color = "Red2";
           item.name = "C";
           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).name
                        group item by ((Item)item).name 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);
       }
   }
   public class GroupInfoList<T> : List<object>
   {

       public object Key { get; set; }


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

最后我想将我的排序数据绑定到gridView

date_for_my_page _date = new date_for_my_page();
List<GroupInfoList<object>> sort_data = _date.GetGroupsByCategory();
CollectionViewSource.Source = sort_data;

输出将显示:

enter image description here 希望这对所有人都有帮助。