WPF:展开/折叠 ListView 中的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16015574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:36:11  来源:igfitidea点击:

WPF: Expand/Collapse items in ListView

wpflistviewexpandablelistview

提问by mburm

I have got the following ListView:

我有以下列表视图:

ListView without hierarchy

没有层次结构的 ListView

At click on the red button from the parent row I want to show the subordinated rows. With a second click the rows should be hidden again.

单击父行中的红色按钮时,我想显示从属行。再次单击应再次隐藏行。

I'm new at WPF and have no idea 1. how to get a row expandable/collapsable and 2. how to create a relationship between parent and children rows.

我是 WPF 的新手,不知道 1. 如何获得可展开/可折叠的行和 2. 如何在父行和子行之间创建关系。

My XAML is the following:

我的 XAML 如下:

<ListView Name="lvUpgrade">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="20px">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="{Binding Path=Icon}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="75px" DisplayMemberBinding="{Binding Path=Time, StringFormat={}{0:HH:mm:ss}}" />
            <GridViewColumn Width="300px" Header="Nachricht" DisplayMemberBinding="{Binding Path=Message}" />
        </GridView>
    </ListView.View>
</ListView>

The code behind:

背后的代码:

Public Class Upgrade

    Public Sub AddMessage(ByVal message As Message)
        Me.lvUpgrade.Items.Add(message)
    End Sub

    Public Class Message

        Public Enum MessageType

            Normal
            Information
            Success
            Warning
            [Error]
        End Enum

        Public Sub New(ByVal type As MessageType, ByVal message As String)
            _Type = type
            _Message = message
        End Sub

        Private _Type As MessageType = MessageType.Normal
        Public ReadOnly Property Type As MessageType
            Get
                Return _Type
            End Get
        End Property

        Private _Message As String = String.Empty
        Public ReadOnly Property Message As String
            Get
                Return _Message
            End Get
        End Property

        Private _Time As DateTime = Now
        Public ReadOnly Property Time As DateTime
            Get
                Return _Time
            End Get
        End Property

        Public ReadOnly Property Icon As BitmapImage
            Get
                Select Case Me.Type
                    Case MessageType.Information
                        Return My.Resources.Information16.ToBitmapImage
                    Case MessageType.Success
                        Return My.Resources.OK16.ToBitmapImage
                    Case MessageType.Warning
                        Return My.Resources.Alert16.ToBitmapImage
                    Case MessageType.Error
                        Return My.Resources.Error16.ToBitmapImage
                    Case Else
                End Select

                Return Nothing
            End Get
        End Property
    End Class
End Class

回答by Felix C

You need the TreeViewcontrol.

你需要TreeView控制。

First link available on google for "wpf treeview tutorial": http://www.howdoicode.net/2011/10/wpf-treeview-example-part-4.html

谷歌上可用的第一个链接“wpf 树视图教程”:http: //www.howdoicode.net/2011/10/wpf-treeview-example-part-4.html