简体   繁体   English

列表视图排序箭头vb.net

[英]List view sort arrow vb.net

i am trying to draw sort arrows on list view column header along with the default visual styles 我正在尝试在列表视图列标题上绘制排序箭头以及默认的视觉样式

so far i have got this 到目前为止,我已经知道了

Private Sub List_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles List.DrawColumnHeader

    e.DrawDefault = True
    If e.ColumnIndex = selectedIndex Then
        e.Graphics.DrawImage(ImageList1.Images(1), CType(e.Bounds.Left + e.Bounds.Width / 2, Single) - 5, -2)
    End If

End Sub

but the visual style is drawn over the arrow somehow so i figured i could try this : 但是视觉风格是以某种方式绘制在箭头上的,所以我认为我可以尝试这样做:

Private Sub List_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles List.DrawColumnHeader

    e.DrawDefault = True
    If lastDrawn.ColumnIndex = selectedIndex Then
        e.Graphics.DrawImage(ImageList1.Images(1), CType(lastDrawn.Bounds.Left + lastDrawn.Bounds.Width / 2, Single) - 5, -2)
    End If

    lastDrawn=e

End Sub

and it draws the arrow when the next corresponding column is being drawn but with this i cant get it to draw for the last column 当下一个对应的列被绘制时它会绘制箭头,但是我无法为最后一列绘制它

Screenshots: 截图: 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

In order to use the .NET build in solution for showing a custom icon for a list view column header you need to: 为了使用.NET内置解决方案显示列表视图列标题的自定义图标,您需要:

  • create an ImageList 创建一个ImageList
  • add three images (up / down arrow and empty) to it 添加三个图像(向上/向下箭头和空白)
  • bind the image list to the ListView control 将图像列表绑定到ListView控件
  • bind to the ColumnClick event of the ListView control 绑定到ListView控件的ColumnClick事件
  • when sorting the columns set the ImageKey property of the currently sorted column depending on the sorting direction 在对列进行排序时,根据排序方向设置当前已排序列的ImageKey属性

This example class (a simple form) shows how to set the images correctly not using custom drawing for the ListView header columns. 该示例类(一种简单的形式)显示了如何正确地设置图像,而不使用ListView标题列的自定义绘图。

It does not implement any sort! 它没有实现任何形式! (how to implement a ListViewSorter is shown in this MSDN article ) (此MSDN文章中显示了如何实现ListViewSorter)

You need to implement a custom ListView-Sorter class and retrieve the image or the image key from it, after a column is sorted. 在对列进行排序之后,您需要实现一个自定义ListView-Sorter类并从中检索图像或图像键。

Public Class SimpleForm
    Inherits Form

    Private sortItems = New ImageList()
    Dim lv As ListView = New ListView()
    Dim so = System.Windows.Forms.SortOrder.Ascending

    Public Sub New()
        ' create columns, items and ListView
        Dim columns = New List(Of ColumnHeader)
        Dim c1 = New ColumnHeader()
        c1.Name = "c1"
        c1.Text = "Name"
        Dim c2 = New ColumnHeader()
        c2.Name = "c2"
        c2.Text = "Type"
        columns.Add(c1)
        columns.Add(c2)

        Dim items = New List(Of ListViewItem)
        Dim i1 = New ListViewItem("Terminator")
        i1.SubItems.Add("T1000")
        Dim i2 = New ListViewItem("Terminator")
        i2.SubItems.Add("T10")
        Dim i3 = New ListViewItem("J.C.")
        i3.SubItems.Add("Human")
        items.Add(i1)
        items.Add(i2)
        items.Add(i3)
        ' init and bind column click
        lv.Columns.AddRange(columns.ToArray())
        lv.Items.AddRange(items.ToArray())
        lv.SmallImageList = sortItems
        lv.View = View.Details
        lv.Dock = DockStyle.Fill
        Controls.Add(lv)

        AddHandler lv.ColumnClick, AddressOf clickEventHandler
        ' init images list 
        sortItems.TransparentColor = System.Drawing.Color.Transparent
        sortItems.Images.Add("up", Image.FromFile("d:\temp\32\arrow_up.gif"))
        sortItems.Images.Add("down", Image.FromFile("d:\temp\32\arrow_down.gif"))
        sortItems.Images.Add("empty", Image.FromFile("d:\temp\32\check.gif"))

    End Sub

    Private Sub clickEventHandler(ByVal o As Object, ByVal e As ColumnClickEventArgs)
        ' Implement a custom ListViewItemSorter and fetch the icon from it!
        ' Set the ListViewItemSorter property to a new ListViewItemComparer  
        ' object. Setting this property immediately sorts the  
        ' ListView using the ListViewItemComparer object. 
        ' THIS CODE SHOWS ONLY HOW TO SET THE SORT ICON!
        For i As Integer = 0 To lv.Columns.Count - 1
            If (i = e.Column) Then
                Select Case (so)
                    Case System.Windows.Forms.SortOrder.Ascending
                        lv.Columns(i).ImageKey = "up"
                        so = System.Windows.Forms.SortOrder.Descending
                    Case System.Windows.Forms.SortOrder.Descending
                        lv.Columns(i).ImageKey = "down"
                        so = System.Windows.Forms.SortOrder.Ascending
                    Case Else
                        lv.Columns(i).ImageKey = "empty"
                        so = System.Windows.Forms.SortOrder.None
                End Select
            Else
                lv.Columns(i).ImageKey = "empty"
            End If
        Next i
    End Sub

End Class

The output looks like this: 输出看起来像这样:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM