Grouping items in a grid

I have alarm model objects that have a Repeat collection containing days on which the alarm should be repeated. I want to display alarms in the form of a grid, grouped on the day of the week (for example, Monday, Tuesday, etc.).

And I add all these alarms to the "Alarms" collection

For each alarm in the alarms, I again create alarms for each day in the re-collection of the alarm and add them all to the TotalAlarms collection.

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

And I use linq to group by the DayOfWeek property of the alarm model, which indicates the day on which the alarm should go out.

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

And add this result to groupedItemsViewSource (binding to grid data source)

 groupedItemsViewSource.Source = result; 

and for the grid title, binding it to the "key"

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

This approach displays only the days of the week for which there is an alarm. For example, if the alarm is set on Friday and Saturday, only Friday and Saturday are displayed in group headings.

What I want is all the fates that are shown as group headlines, and if there are no worries on that day, then it can be empty. But group headings should be displayed all days.

It's really hard for me to think about how to do this. If anyone has any ideas please help me here.

thanks

+4
source share
3 answers

I finally understood the answer myself. Its a little dirty and ugly, but it works. Instead of 7, I use 7 types of mesh. But it still does not display the title if there is no data element in the grid view. SO, if the grid collection is not empty, I add an empty object to the collection, snapping to the grid view, and then reduce the height of the grid view so that the empty element is not displayed and only the title is displayed.

This is an example code for one kind of grid.

  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; 

And XAMl code for one kind of grid

  <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> 
0
source

Take a look at the following articles: http://code.msdn.microsoft.com/windowsapps/Push-and-periodic-de225603

http://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh868244(v=win.10).aspx

Push notifications may be periodically or a special condition.

0
source

I use this for my project

Note: I want to group by one of the fields in my table

My model:

  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)); } } } 

in 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> 

And in viewModel this code is needed:

  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(); } } 

Recently, I want to bind my sorted data to my gridView

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

The result will display this:

enter image description here Hope this has helped everyone.

0
source

All Articles