2012-12-11 70 views
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 to組報警模式的DAYOFWEEK財產這表明在其上報警應熄滅的那一天。

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

和AM添加此結果給groupedItemsViewSource(網格視圖結合的ItemSource)

groupedItemsViewSource.Source = result; 

和用於網格視圖的報頭,AM結合爲 「密鑰」

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

此方法僅顯示出現鬧鐘的星期幾。就像警報設置爲星期五和星期六一樣,組標題中僅顯示星期五和星期六。

我想要的是所有的日子顯示爲組頭,如果沒有警報的那一天,那麼它可以是空的。但是組標題應該顯示所有的日子。

我真的覺得很難想辦法做到這一點。如果任何人有任何想法,請幫我在這裏....

感謝

回答

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

而且在視圖模型,需要此代碼:

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 希望這個幫助編輯給所有人。

相關問題