winforms c#当我最大化列表视图时,如何使列表视图的列占据整个窗口?

wpx232ag  于 2022-12-14  发布在  C#
关注(0)|答案(1)|浏览(192)

嗨,我需要调整列标题,使它们占据整个窗口时,它是最大化。
预期的输出是列表视图列占据整个窗口,但真实的输出是列不占据整个窗口,否则当窗口最大化时,列占据窗口的一部分。
当我声明方法初始化列表视图并调整其各自列的大小时,这是一个类(当我将其最大化时,这些方法不会占据整个窗口)。

public Takenshows()
{
InitializeComponent();
lvwColumnSorter = new ListViewColumnSorter();
this.listView1.ListViewItemSorter = lvwColumnSorter;
listView1.Dock = System.Windows.Forms.DockStyle.Fill; //The table occupies the entire width of the window when the window is maximized.
}

//this is a helper method where we REFIT the sampler table by maximizing it
private void SizeLastColumn(System.Windows.Forms.ListView lv)
{
lv.Columns[lv.Columns.Count - 1].Width = -2;
lv.Columns[lv.Columns.Count - 1].AutoResize(ColumnHeaderAutoResizeStyle.None);
lv.Dock = System.Windows.Forms.DockStyle.Fill;
lv.Alignment = ListViewAlignment.SnapToGrid;
}

//A method that allows you to wrap the table by calling the helper method that wraps the table when the window is maximized.
private void listView1_Resize(object sender, System.EventArgs e)
{
SizeLastColumn((System.Windows.Forms.ListView)sender);
}

private void Takenshows_Load(object sender, EventArgs e)
{
// INITIALIZE the table

listView1.View = View.Details;
SizeLastColumn(listView1);
}

这是Takenshows.cs的设计器代码的一部分,其中包含列表视图和它们各自的列标题。

public System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnordnum;
private System.Windows.Forms.ColumnHeader columndatetimeshow;
private System.Windows.Forms.ColumnHeader columnvalues;
private System.Windows.Forms.ColumnHeader columntestnumber;

this.listView1.Alignment = System.Windows.Forms.ListViewAlignment.SnapToGrid;
this.listView1.AllowColumnReorder = true;
this.listView1.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.columnordnum,
        this.columndatetimeshow,
        this.columnvalues,
        this.columntestnumber});
this.listView1.FullRowSelect = true;
this.listView1.HideSelection = false;
this.listView1.Location = new System.Drawing.Point(3, 4);
this.listView1.Margin = new System.Windows.Forms.Padding(5);
this.listView1.MultiSelect = false;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(1064, 559);
this.listView1.AllowColumnReorder = true;
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.TabIndex = 1;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.SelectedIndexChanged += new 
System.EventHandler(this.listView1_SelectedIndexChanged);
// 
// columnordnum
// 
this.columnordnum.Text = "Order number";
this.columnordnum.Width = 115;
this.columnordnum.AutoResize(System.Windows.Forms.ColumnHeaderAutoResizeStyle.None);
// 
// columndatetimeshow
// 
this.columndatetimeshow.Text = "datetime show";
this.columndatetimeshow.Width = 140;
this.columndatetimeshow.AutoResize(System.Windows.Forms.ColumnHeaderAutoResizeStyle.None);
// 
// columnvalues
// 
this.columnvalues.Text = "values";
this.columnvalues.Width = 420;
this.columnvalues.AutoResize(System.Windows.Forms.ColumnHeaderAutoResizeStyle.None);
// 
// columntestnumber
// 
this.columntestnumber.Text = "test number";
this.columntestnumber.Width = 105;
this.columntestnumber.AutoResize(System.Windows.Forms.ColumnHeaderAutoResizeStyle.None);

我应该呈现列表视图,这样列表视图的列在最大化时占据整个窗口。
有什么办法可以解决这个问题吗?

a5g8bdjr

a5g8bdjr1#

您的目标是缩放ListView列,使其在最大化(或通常调整大小)时填充主窗口的整个宽度。假设ListView是锚定的,订阅ListView在调整大小时触发的事件将允许您根据窗口的新宽度重新计算成比例(或自定义)的宽度。例如:

public partial class MainForm : Form
{
    public MainForm() => InitializeComponent();
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        autoScaleColumns();
        listView.Anchor = (AnchorStyles)0xF;
        listView.SizeChanged += (sender, e) => autoScaleColumns();
    }
    private void autoScaleColumns()
    {
        var proportional = listView.ClientRectangle.Width / listView.Columns.Count;
        foreach (ColumnHeader column in listView.Columns)
        {
            column.Width = proportional;
        }
    }
}

我还鼓励您考虑使用DataGridView的替代方法,它具有更灵活的内置AutoSize属性。

相关问题