我将这个用于我的项目
注意:我想按我的表的字段之一分组
我的模型是:
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;
输出将显示:
希望这对所有人都有帮助。