开发者

WinForms ListView: How to specify width of a ListViewItem when there are columns but in View.List display mode?

开发者 https://www.devze.com 2023-01-29 12:24 出处:网络
I have a System.Windows.Forms.ListView control that I was using with View = View.List.The ListViewItems I was adding did not have any subitems, only text and an icon.This worked fine.But I wanted to a

I have a System.Windows.Forms.ListView control that I was using with View = View.List. The ListViewItems I was adding did not have any subitems, only text and an icon. This worked fine. But I wanted to allow the user to change the View to View.Details at runtime. So I added columns to the ListView control, subitems to the ListViewItems, and a button to allow the user to change the View. The Detail View looks fine but when in List mode, the text of the ListViewItems are truncated to only the first letter and have ellipses. For example if the text of the ListViewItem is "stackoverflow" it appears as "s...". And the user cannot resize the item. How can开发者_如何学运维 I make it so that when in View.List mode the text of the ListViewItems appear like they did before I added the columns and the subitems to the ListViewItems?


I see it. Make the column wider to see the effect it has. The native Windows control is getting confuzzled by seeing the header control getting created even though it is in list mode. Short from making the column wider, the only real workaround is to remove the column before switching back to View = List. I recommend the latter approach, that header control might have some additional side-effects.

    private void button1_Click(object sender, EventArgs e) {
        if (listView1.View == View.List) {
            listView1.View = View.Details;
            listView1.Columns.Add(new ColumnHeader());
        }
        else {
            listView1.Columns.Clear();
            listView1.View = View.List;
        }
    }


Cannot reproduce your problem, my guess is you need to double check the ListViewItem you were created and added to the Listview.


We take an example of ListView with 2 columns and resize on contents and then to minimum width.

// Auto resize of ListView Columns to minimum width
private int[] ColumnsWidth = { 35, 322 };

/// <summary>
/// Resize the columns based on the items entered
/// </summary>
private void ResizeColumns()
{
    // Auto Resize Columns based on content
    m_urlsListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
    // Make sure to resize to minimum width
    if (m_urlsListView.Columns[0].Width < ColumnsWidth[0])
    {
        m_urlsListView.Columns[0].Width = ColumnsWidth[0];
    }
    if (m_urlsListView.Columns[1].Width < ColumnsWidth[1])
    {
        m_urlsListView.Columns[1].Width = ColumnsWidth[1];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消