Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ListViewItemSorter"/> class
        /// that uses the specified comparers to sort columns
        /// and attaches the new instance to the specified ListView.
        /// </summary>
        /// <param name="listView">The ListView that should be sorted and monitored for column click events.</param>
        /// <param name="itemComparers">An array of objects that implement IListViewItemComparer.
        /// The index of the comparer in the array corresponds to the index of the list view column that it sorts.
        /// If an element is <c>null</c>, the corresponding column cannot be sorted.
        /// If this parameter is <c>null</c>, all columns can be sorted by their text content.</param>
        /// <remarks>
        /// Note that the ListViewItemSorter constructor adds two bitmaps to the
        /// SmallImageList of the ListView (an empty ImageList will be created
        /// first if none is assigned).
        /// </remarks>
        public ListViewItemSorter(ListView listView, IListViewItemComparer[] itemComparers)
        {
            if (listView == null)
            {
                throw new ArgumentNullException("listView");
            }
            this.listView = listView;

            if (itemComparers == null)
            {
                IListViewItemComparer defaultComparer = new ListViewTextColumnComparer();
                this.itemComparers = new IListViewItemComparer[this.ListView.Columns.Count];
                for (int i = 0; i < this.itemComparers.Length; i++)
                {
                    this.itemComparers[i] = defaultComparer;
                }
            }
            else
            {
                this.itemComparers = itemComparers;
            }

            if (this.ListView.SmallImageList == null)
            {
                this.ListView.SmallImageList = new ImageList();
            }
            this.ListView.SmallImageList.Images.Add("SortAscending", BitmapResources.GetBitmap("Icons.16x16.SortAscending.png"));
            this.ListView.SmallImageList.Images.Add("SortDescending", BitmapResources.GetBitmap("Icons.16x16.SortDescending.png"));

            this.ListView.ColumnClick       += this.ListViewColumnClick;
            this.ListView.ListViewItemSorter = this;
        }