0

我有一个面板,其中包含一个TableLayoutPanel本身包含多个ListViews和的面板Labels

我想要的是每个列表视图调整大小以垂直适应它的所有内容(即每一行都是可见的)。应该处理任何垂直滚动,TableLayoutPanel但我不知道如何ListView根据行数调整自身大小。

我需要处理OnResize和手动调整大小还是已经有东西可以处理?

4

1 回答 1

0

一个类似的问题建议使用ObjectList,但它看起来对我想要的东西来说有点过分了。因此,我做了这个简单的重载(如下)来根据列表中的项目调整大小。

我只在 Windows Vista 上以详细信息模式测试过这种显示,但它很简单,而且似乎运行良好。

#pragma once

/// <summary> 
/// A ListView based control which adds a method to resize itself to show all
/// items inside it.
/// </summary>
public ref class ResizingListView :
public System::Windows::Forms::ListView
{
public:
    /// <summary>
    /// Constructs a ResizingListView
    /// </summary>
    ResizingListView(void);

    /// <summary>
    /// Works out the height of the header and all the items within the control
    /// and resizes itself so that all items are shown.
    /// </summary>
    void ResizeToItems(void)
    {
        // Work out the height of the header
        int headerHeight = 0;
        int itemsHeight = 0;
        if( this->Items->Count == 0 )
        {
            // If no items exist, add one so we can use it to work out
            this->Items->Add("");
            headerHeight = GetHeaderSize();
            this->Items->Clear();

            itemsHeight = 0;
        }
        else
        {
            headerHeight = GetHeaderSize();
            itemsHeight = this->Items->Count*this->Items[0]->Bounds.Height;
        }

        // Work out the overall height and resize to it
        System::Drawing::Size sz = this->Size;
        int borderSize = 0;
        if( this->BorderStyle != System::Windows::Forms::BorderStyle::None )
        {
            borderSize = 2;
        }
        sz.Height = headerHeight+itemsHeight+borderSize;
        this->Size = sz;
    }

protected:
    /// <summary>
    /// Grabs the top of the first item in the list to work out how tall the
    /// header is. Note: There _must_ at least one item in the list or an 
    /// exception will be thrown
    /// </summary>
    /// <returns>The height of the header</returns>
    int GetHeaderSize(void)
    {
        return Items[0]->Bounds.Top;
    }


};
于 2010-06-18T09:22:53.487 回答