4

アラームが繰り返される日を含む Repeat コレクションを持つアラーム モデル オブジェクトがあります。曜日 (月曜日、火曜日など) の下にグループ化されたグリッド ビューにアラームを表示したいと考えています。

そして、これらすべてのアラームをコレクション「アラーム」に追加しています

アラームのアラームごとに、アラームの繰り返しコレクションで毎日アラームを作成し、それらすべてをコレクション「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;

グリッドビューのヘッダーについては、「キー」にバインドしています

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

この方法では、アラームがある曜日のみが表示されます。アラームが金曜日と土曜日に設定されている場合と同様に、金曜日と土曜日のみがグループ ヘッダーに表示されます。

私が望むのは、グループ ヘッダーとして表示されるすべての日であり、その日のアラームがない場合は空になる可能性があります。ただし、グループ ヘッダーはすべての日を表示する必要があります。

その方法を考えるのは本当に難しいと思います。誰かが何か考えを持っているなら、ここで私を助けてください....

ありがとう

4

3 に答える 3

0

私は自分のプロジェクトにこれを使用します

注: テーブルのフィールドの 1 つによってグループ化したい

私のモデルは次のとおりです。

 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 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 に答える
0

私はついに自分で答えを見つけました。少し面倒で醜いですが、機能します。現在、1 つではなく 7 つのグリッド ビューを使用しています。ただし、グリッド ビューにデータ項目がない限り、ヘッダーは表示されません。SO グリッド ビュー コレクションが空の場合、グリッド ビューをバインドするコレクションに空のオブジェクトを追加し、グリッド ビューの高さを減らして、空のアイテムが表示されず、ヘッダーのみが表示されるようにします。

これは、1 つのグリッド ビューのサンプル コードです。

 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;

1 つのグリッド ビューの 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

この記事を見てください: 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 に答える