1

静的な幅の列幅が計算された後、残りのサイズに基づいて ListView の特定の列のみのサイズを変更しようとしています。たとえば、数量、価格などの列のサイズを変更したくはありませんが、名前や説明など、通常は幅の広いデータを含む列の幅を広げたいと考えています。これは私が以下で使用しようとしている方法ですが、機能していません。

ところで、ListView で ClientSizeChanged イベントが発生したときに、このメソッドを呼び出しています。それが関連しているかどうかはわかりません。

    public static void ResizeListViewColumns(ListView lv, List<int> fixedColumnIndexes, List<int> nonFixedColumnIndexes)
    {
        int lvFixedWidth = 0;
        int lvNonFixedWidth = 0;
        if (fixedColumnIndexes.Count + nonFixedColumnIndexes.Count != lv.Columns.Count)
        {
            throw new Exception("Number of columns to resize does not equal number of columns in ListView");
        }
        else
        {
            // Calculate the fixed column width
            // Calculate the non-fixed column width
            // Calculate the new width of non-fixed columns by dividing the non-fixed column width by number of non-fixed columns
            foreach (var fixedColumn in fixedColumnIndexes)
            {
                lvFixedWidth += lv.Columns[fixedColumn].Width;
            }

            foreach (var nonFixedColumn in nonFixedColumnIndexes)
            {
                lvNonFixedWidth += lv.Columns[nonFixedColumn].Width;
            }

            int numNonFixedColumns = nonFixedColumnIndexes.Count;
            foreach (var newNonFixedColumn in nonFixedColumnIndexes)
            {
                lv.Columns[newNonFixedColumn].Width = lvNonFixedWidth / numNonFixedColumns;
            }
        }
    }
4

1 に答える 1

1

このようなものが必要です...固定インデックスと非固定インデックスのリストを保持する代わりに、私の例では、各固定列の「タグ」プロパティを文字列「固定」に設定します。

int fixedWidth = 0;
        int countDynamic = 0;

        for (int i = 0; i < listView1.Columns.Count; i++)
        {
            ColumnHeader header = listView1.Columns[i];

            if (header.Tag != null && header.Tag.ToString() == "fixed")
                fixedWidth += header.Width;
            else
                countDynamic++;
        }

        for (int i = 0; i < listView1.Columns.Count; i++)
        {
            ColumnHeader header = listView1.Columns[i];

            if (header.Tag == null)
                header.Width = ((listView1.Width - fixedWidth) / countDynamic);
        }
于 2012-10-16T19:51:25.633 に答える