4

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

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

对于警报中的每个警报,我再次在警报的重复集合中为每一天创建警报,并将它们全部添加到集合“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"/>

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

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

我真的很难想出一种方法来做到这一点。如果有人有任何想法,请在这里帮助我......

谢谢

4

3 回答 3

0

看看这篇文章: 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

推送通知,可以是定期或特殊条件。

于 2012-12-12T20:56:22.497 回答
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>
于 2012-12-18T06:43:12.733 回答
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;

输出将显示:

在此处输入图像描述 希望这对所有人都有帮助。

于 2015-12-27T12:54:49.023 回答