ListView.TopItem 属性

定义

获取或设置控件中的第一个可见项。

C#
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ListViewItem TopItem { get; }
C#
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ListViewItem TopItem { get; set; }
C#
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ListViewItem? TopItem { get; set; }

属性值

ListViewItem,表示控件中的第一个可见项。

属性

例外

示例

下面的代码示例演示如何通过使用 属性和 ListViewItem.ListViewSubItem.ResetStyle 方法重置对象的子项的TopItem样式ListViewItem。 若要运行该示例,请将以下代码粘贴到包含名为 Button1 的按钮的窗体中,并在窗体的构造函数或Load事件处理程序中调用 InitializeListView 方法。

C#

// Declare the Listview object.
internal System.Windows.Forms.ListView myListView;

// Initialize the ListView object with subitems of a different
// style than the default styles for the ListView.
private void InitializeListView()
{

    // Set the Location, View and Width properties for the 
    // ListView object. 
    myListView = new ListView();
    myListView.Location = new System.Drawing.Point(20, 20);
    myListView.Width = 250;

    // The View property must be set to Details for the 
    // subitems to be visible.
    myListView.View = View.Details;
    
    // Each SubItem object requires a column, so add three columns.
    this.myListView.Columns.Add("Key", 50, HorizontalAlignment.Left);
    this.myListView.Columns.Add("A", 100, HorizontalAlignment.Left);
    this.myListView.Columns.Add("B", 100, HorizontalAlignment.Left);

    // Add a ListItem object to the ListView.
    ListViewItem entryListItem = myListView.Items.Add("Items");

    // Set UseItemStyleForSubItems property to false to change 
    // look of subitems.
    entryListItem.UseItemStyleForSubItems = false;

    // Add the expense subitem.
    ListViewItem.ListViewSubItem expenseItem = 
        entryListItem.SubItems.Add("Expense");

    // Change the expenseItem object's color and font.
    expenseItem.ForeColor = System.Drawing.Color.Red;
    expenseItem.Font = new System.Drawing.Font(
        "Arial", 10, System.Drawing.FontStyle.Italic);

    // Add a subitem called revenueItem 
    ListViewItem.ListViewSubItem revenueItem = 
        entryListItem.SubItems.Add("Revenue");

    // Change the revenueItem object's color and font.
    revenueItem.ForeColor = System.Drawing.Color.Blue;
    revenueItem.Font = new System.Drawing.Font(
        "Times New Roman", 10, System.Drawing.FontStyle.Bold);

    // Add the ListView to the form.
    this.Controls.Add(this.myListView);
}

private void Button1_Click(System.Object sender, System.EventArgs e)
{

    // Use the ListView.TopItem property to access the SubItems
    // and then reset their appearance.
    myListView.TopItem.SubItems[1].ResetStyle();
    myListView.TopItem.SubItems[2].ResetStyle();
}

注解

最初,索引位置为 0 的项 (0) 位于控件的 ListView 顶部。 ListView如果滚动控件内容,则控件顶部可以有其他项。 可以使用此属性来指示或确定在控件顶部可见的 ListView 项。 属性的值 TopItem 不会始终保留,具体取决于所需顶部项在列表视图中的位置。

控件中 ListView 随时可见的项数取决于列表视图的高度及其包含的项的大小。 如果项超过列表视图的高度,则这些项将继续到多个页面上,用户可以滚动浏览这些页面。 如果将 属性设置为 TopItem 的最后一页 ListView中的某个项,则会自动滚动到视图中;但是, TopItem 将设置为最后一页的实际顶部项。

若要确保特定项位于控件 (的可见区域,但不一定位于) 的顶部位置,请使用 EnsureVisible 方法。

备注

当属性值为 false时,Scrollable设置此属性不起作用。

在版本 2.0 之前的 .NET Framework 版本中不支持设置此属性。

适用于

产品 版本
.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

另请参阅