Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace Test
- {
- public class GroupKeyList<T> : ObservableCollection<T>, INotifyPropertyChanged
- {
- public string Key { get; private set; }
- // Declare the event
- public new event PropertyChangedEventHandler PropertyChanged;
- // Create the OnPropertyChanged method to raise the event
- protected void OnPropertyChanged(string name)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null)
- {
- handler(this, new PropertyChangedEventArgs(name));
- }
- }
- public delegate string GetKeyDelegate(T item);
- public static ObservableCollection<GroupKeyList<T>> CreateGroupList(ObservableCollection<T> items, GetKeyDelegate getKey)
- {
- ObservableCollection<GroupKeyList<T>> new_group_list = new ObservableCollection<GroupKeyList<T>>(); // create the list
- foreach (T item in items)
- {
- string key = getKey(item); // get key
- // search to see if the key has been added to the group
- GroupKeyList<T> new_item = new_group_list.FirstOrDefault(delegate(GroupKeyList<T> t) { return t.Key == key; });
- if (new_item == null)
- {
- new_item = new GroupKeyList<T>(); // create the group
- new_item.Key = key; // set the key
- new_group_list.Add(new_item); // add the new group
- new_item.Add(item); // add the item to the new group
- }
- else
- {
- new_item.Add(item); // add the item to the existing group (matching)
- }
- }
- return new_group_list;
- }
- public T FindMatch(string pattern, GetKeyDelegate getKey)
- {
- Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
- foreach (T item in this.Items)
- {
- string key = getKey(item);
- Match match = rgx.Match(key);
- if (match.Success)
- return item;
- }
- return default(T);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement