.Net 的高度可定制列表视图?

Mil*_*ike 1 .net vb.net customization listview replace

你们知道一个可以高度定制的.net ListView-Replacement 吗?我正在开发一个类似于“待办事项”列表的应用程序。我希望能够自定义列表的几乎每个视觉细节,所以我得到这样的结果:

列表显示

我还希望能够通过鼠标重新排序项目+我需要拖放。如果您不知道任何 ListView,也许您知道我如何从头开始制作像这样的自己的列表视图?

感谢您的想法!

Ste*_*fan 5

使用 flowlayoutpanel 控件作为容器,然后创建一个作为列表项的用户控件,你就成功了一半。用户控件可以具有您想要的任何外观,并将充当流程布局面板中的列表项。然后我们进行拖放。按照此代码(最初来自此示例:http://www.codeproject.com/KB/static/DragDropFlowLayoutPanel.aspx),您将在 flowlayoutpanel 中添加项目的拖放:

首先创建一个新的windform解决方案,然后创建一个仅包含标签的用户控件,我们使用它作为特殊列表项控件的示例。将控件命名为 MyListItem。将此代码粘贴到用户控件中以使其可拖放:

Public Class MyListItem
    Public Property AllowDrag() As Boolean
        Get
            Return m_AllowDrag
        End Get
        Set(ByVal value As Boolean)
            m_AllowDrag = value
        End Set
    End Property
    Private m_AllowDrag As Boolean
    Private _isDragging As Boolean = False
    Private _DDradius As Integer = 40
    Private _mX As Integer = 0
    Private _mY As Integer = 0

    Public Sub New()
        InitializeComponent()
        Margin = New Padding(0)
        AllowDrag = True
    End Sub

    Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
        Me.BackColor = Color.Navy
    End Sub

    Protected Overrides Sub OnLostFocus(ByVal e As EventArgs)
        Me.BackColor = Color.Transparent

    End Sub

    Protected Overrides Sub OnClick(ByVal e As EventArgs)
        Me.Focus()
    End Sub

    Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
        Me.Focus()
        _mX = e.X
        _mY = e.Y
        Me._isDragging = False
    End Sub

    Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
        If Not _isDragging Then
            If e.Button = MouseButtons.Left AndAlso _DDradius > 0 AndAlso Me.AllowDrag Then
                Dim num1 As Integer = _mX - e.X
                Dim num2 As Integer = _mY - e.Y
                If ((num1 * num1) + (num2 * num2)) > _DDradius Then
                    DoDragDrop(Me, DragDropEffects.All)
                    _isDragging = True
                    Return
                End If
            End If
        End If
    End Sub
    Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
        _isDragging = False
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

然后将 flowlayoutpanel (flowlayoutpanel1) 放置到应用程序的主窗体上。

将此代码添加到表单中,它将使用您可以拖放的列表项填充 flowlayoutpanel:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FlowLayoutPanel1.AllowDrop = True
        For p As Integer = 0 To 50
            Dim listitem As New MyListItem With {.Height = 50, .BorderStyle = BorderStyle.FixedSingle}
            listitem.Label1.Text = "Item:" & p.ToString
            FlowLayoutPanel1.Controls.Add(listitem)
        Next
        AddHandler FlowLayoutPanel1.DragEnter, AddressOf flowLayoutPanel_DragEnter
        AddHandler FlowLayoutPanel1.DragDrop, AddressOf flowLayoutPanel1_DragDrop        
    End Sub

    Sub flowLayoutPanel_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
        e.Effect = DragDropEffects.All
    End Sub

    Private Sub flowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
        Dim data As MyListItem = CType(e.Data.GetData(GetType(MyListItem)), MyListItem)
        Dim _destination As FlowLayoutPanel = CType(sender, FlowLayoutPanel)
        Dim _source As FlowLayoutPanel = CType(data.Parent, FlowLayoutPanel)
        If sender.Equals(data.Parent) Then
            Dim p As Point = _destination.PointToClient(New Point(e.X, e.Y))
            Dim item = _destination.GetChildAtPoint(p)
            Dim index As Integer = _destination.Controls.GetChildIndex(item, False)
            _destination.Controls.SetChildIndex(data, index)
            _destination.Invalidate()
        End If
    End Sub
Run Code Online (Sandbox Code Playgroud)

现在您可以启动程序并进行测试。您现在拥有一个“列表视图”,其中可以包含自定义控件,并允许您拖放项目来更改项目顺序。

感谢 P.Sandgren 在 flowlayout 面板中拖放项目:
http://www.codeproject.com/KB/static/DragDropFlowLayoutPanel.aspx