英語で読む

次の方法で共有


ListView.ListViewItemSorter プロパティ

定義

コントロールの並べ替え比較子を取得または設定します。

[System.ComponentModel.Browsable(false)]
public System.Collections.IComparer ListViewItemSorter { get; set; }
[System.ComponentModel.Browsable(false)]
public System.Collections.IComparer? ListViewItemSorter { get; set; }

プロパティ値

コントロールの並べ替え比較子を表す IComparer

属性

次のコード例では、コントロール内の列がクリックされたときに項目を ListView 手動で並べ替えるコントロールを含むフォームを ListView 作成します。 この例では、比較を実行する インターフェイスをSystem.Collections.IComparer実装する というListViewItemComparerクラスをListViewItem定義します。 この例では、 のListViewItemComparerインスタンスを作成し、それを使用してコントロールの プロパティをListViewItemSorterListView設定します。 イベント ハンドラーのColumnClickメソッド呼び出しではSort、 でListViewItemComparer定義されているメソッドを使用して、クリックされた列に基づいて項目の並べ替えを実行します。

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;

namespace ListViewSortFormNamespace
{

    public class ListViewSortForm : Form
    {
        private ListView listView1;
       
        public ListViewSortForm()
        {
            // Create ListView items to add to the control.
            ListViewItem listViewItem1 = new ListViewItem(new string[] {"Banana","a","b","c"}, -1, Color.Empty, Color.Yellow, null);
            ListViewItem listViewItem2 = new ListViewItem(new string[] {"Cherry","v","g","t"}, -1, Color.Empty, Color.Red, new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((System.Byte)(0))));
            ListViewItem listViewItem3 = new ListViewItem(new string[] {"Apple","h","j","n"}, -1, Color.Empty, Color.Lime, null);
            ListViewItem listViewItem4 = new ListViewItem(new string[] {"Pear","y","u","i"}, -1, Color.Empty, Color.FromArgb(((System.Byte)(192)), ((System.Byte)(128)), ((System.Byte)(156))), null);
     
            //Initialize the ListView control and add columns to it.
            this.listView1 = new ListView();

            // Set the initial sorting type for the ListView.
            this.listView1.Sorting = SortOrder.None;
            // Disable automatic sorting to enable manual sorting.
            this.listView1.View = View.Details;
            // Add columns and set their text.
            this.listView1.Columns.Add(new ColumnHeader());
            this.listView1.Columns[0].Text = "Column 1";
            this.listView1.Columns[0].Width = 100;
            listView1.Columns.Add(new ColumnHeader());
            listView1.Columns[1].Text = "Column 2";
            listView1.Columns.Add(new ColumnHeader());
            listView1.Columns[2].Text = "Column 3";
            listView1.Columns.Add(new ColumnHeader());
            listView1.Columns[3].Text = "Column 4";
            // Suspend control logic until form is done configuring form.
            this.SuspendLayout();
            // Add Items to the ListView control.
            this.listView1.Items.AddRange(new ListViewItem[] {listViewItem1,
                listViewItem2,
                listViewItem3,
                listViewItem4});
            // Set the location and size of the ListView control.
            this.listView1.Location = new Point(10, 10);
            this.listView1.Name = "listView1";
            this.listView1.Size = new Size(300, 100);
            this.listView1.TabIndex = 0;
            // Enable editing of the items in the ListView.
            this.listView1.LabelEdit = true;
            // Connect the ListView.ColumnClick event to the ColumnClick event handler.
            this.listView1.ColumnClick += new ColumnClickEventHandler(ColumnClick);
            
            // Initialize the form.
            this.ClientSize = new Size(400, 400);
            this.Controls.AddRange(new Control[] {this.listView1});
            this.Name = "ListViewSortForm";
            this.Text = "Sorted ListView Control";
            // Resume layout of the form.
            this.ResumeLayout(false);
        }

        // ColumnClick event handler.
        private void ColumnClick(object o, ColumnClickEventArgs e)
        {
            // Set the ListViewItemSorter property to a new ListViewItemComparer 
            // object. Setting this property immediately sorts the 
            // ListView using the ListViewItemComparer object.
            this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
        }

        [System.STAThreadAttribute()]
        public static void Main()
        {
            Application.Run(new ListViewSortForm());
        }
    }

    // Implements the manual sorting of items by columns.
    class ListViewItemComparer : IComparer
    {
        private int col;
        public ListViewItemComparer()
        {
            col = 0;
        }
        public ListViewItemComparer(int column)
        {
            col = column;
        }
        public int Compare(object x, object y)
        {
            return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
        }
    }
}

注釈

ListViewItemSorterプロパティを使用すると、メソッドが呼び出されたとき、または項目がリストに追加されたときにSort、コントロール内ListViewの項目の並べ替えを実行するオブジェクトを指定できます。 ラベル テキストが変更された場合、アイテムは自動的に並べ替えされないことに注意してください。

指定した オブジェクトは、 インターフェイスを実装 IComparer するクラスのインスタンスである必要があります。このインスタンスには、 という名前 Comparer.Compareのメソッドが 1 つ含まれています。

このプロパティを使用すると、詳細ビューで列ヘッダーをクリックした場合など、カスタムの並べ替えを指定できます。 これを行うには、 インターフェイスを実装 IComparer し、並べ替えの基準となる列のインデックスを受け入れるコンストラクターを提供するクラスを作成します。 その後、クリックした列のインデックスを ColumnClick 使用して、このクラスのインスタンスを作成する イベントのハンドラーを実装できます。 プロパティを ListViewItemSorter 新しいインスタンスに設定すると、指定したオブジェクトを ListView 使用してコントロールが自動的に並べ替えられます。 メソッドの後続の呼び出しでは Sort 、同じオブジェクトが使用されます。

注意

プロパティの ListViewItemSorter 値を設定すると、 Sort メソッドが自動的に呼び出されます。

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください