locked
WPF listView inside listView and selection change! RRS feed

  • Question

  • Hi!

    I want to run SelectionChanged Event in listView which is located inside another list view. The code look like this:


    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300"
            Name="Window1">
        <Grid>
            <ListView DataContext="{Binding ElementName=Window1}" 
                      ItemsSource="{Binding ItemList}" 
                      BorderThickness="0"
                      x:Name="itemListView"
                      SelectionChanged="itemListView_SelectionChanged"> 
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ListView DataContext="{Binding ElementName=Window1}" 
                                  ItemsSource="{Binding SubList}"
                                  BorderThickness="0"
                                  x:Name="subListView"
                                  SelectionChanged="subListView_SelectionChanged">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding}"/>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Window>
    Class Window1
    
    
        Private _itemList As List(Of String)
        Public Property ItemList() As List(Of String)
            Get
                Return _itemList
            End Get
            Set(ByVal value As List(Of String))
                _itemList = value
            End Set
        End Property
    
        Private _subList As List(Of String)
        Public Property SubList() As List(Of String)
            Get
                Return _subList
            End Get
            Set(ByVal value As List(Of String))
                _subList = value
            End Set
        End Property
    
    
        Public Sub New()
    
            _itemList = New List(Of String)
            _subList = New List(Of String)
    
            _itemList.Add("item1")
            _itemList.Add("item2")
            _itemList.Add("item3")
    
            _subList.Add("sub1")
            _subList.Add("sub2")
    
            ' This call is required by the Windows Form Designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
    
        End Sub
    
        Private Sub subListView_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
            MsgBox("Selection Changed")
        End Sub
    End Class




    If user change selection in subListView the SelectionChanged event should be run.
    So if user select sub1 from item1 message box should apper. The same, if he choose sub1 from item2. And it works but only on the first time when user change selection.
    If I try to repeat the action - select sub from item1 nothing happen, because sub1 is still selected in item1 and item2.

    How can I resolve this problem? I need to run selectionChanged event whenever user change his selection.

    Thanks in advance for your help.!
    Friday, October 30, 2009 8:33 AM

Answers

  • Hi,
    Actually here what are trying to do?
    In the above sample that you have provided only the item sub1  and sub2 are shown.The items "item" are not shown.
    This is because the first listview is bound to collection ItemList which is not shown coz of its datatemplate which is bound to the SubList property.
    Is it that you want "Item" as a header for each of the subList items.In that case you can very well use a TreeView control also to display it.
    Create a entity class of your own like below

    Class MyData
    
    
        Private lstSubList As List(Of String)
        Public Property SubList() As List(Of String)
            Get
                Return lstSubList
            End Get
            Set(ByVal value As List(Of String))
                lstSubList = value
            End Set
        End Property
    
    
        Private strItemHeader As String
        Public Property ItemHeader() As String
            Get
                Return strItemHeader
            End Get
            Set(ByVal value As String)
                strItemHeader = value
            End Set
        End Property
    
    End Class

    Create list of MyData and bind to the Listview(or Treeview).

    Feel free if to post back if you have any doubts.

    Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem
    • Proposed as answer by Zhi-Xin Ye Wednesday, November 4, 2009 5:49 PM
    • Marked as answer by Zhi-Xin Ye Friday, November 6, 2009 5:48 AM
    Saturday, October 31, 2009 5:59 AM