locked
treeview and selecteditemchanged RRS feed

  • Question

  • Hello,

       I am struggling with managing the events from a selection of a treeview.  I have a two level treeview control as follow:

    Node1
       child1
       child2
       child3

      In my selecteditemchanged event handler I check for the value of the selected item.  I get the following strange behaviour:

    1) If the user expand the node: Node1, I get no events
    2) When the user selects node: "child2", an event is triggered and the value of selected item is child2  (so far so good :-))
    3) When the user collapse Node1, I get an event and the value of selected item is child3  (it's very strange...)

    I am curious if anyone ran into this situation.  Also are there any workarounds you can think of.  Is there a way to know when the event comes from collapsing a parent node.  In my case, collapsing "node1".  The user is not selecting "node1" just collapsing the tree node.

    I thank you in advance for your help,
    Didier
    Thursday, October 22, 2009 9:17 PM

Answers

  • I created a Silverlight project in Blend3 containing a treview named tv and a texblock named tbMessage. Since I find it easier to think in Visual studio, I then opened the project in Visual and created the code below. 

    1. When I run the application, everything works as expected.  In particular, when Node1 is collapsed, the message is node1.

     

     

    Partial Public Class MainPage

     

    Inherits UserControl

     

    Public Sub New()

    InitializeComponent()


    Dim
    node1 As New TreeViewItem

    node1.Name = "First Node"

    node1.Header = "Node1"


    Dim
    child1 As New TreeViewItem

    child1.Name = "A"

    child1.Header = "A"

    node1.Items.Add(child1)


    Dim
    child2 As New TreeViewItem

    child2.Name = "B"

    child2.Header = "B"

    node1.Items.Add(child2)


    Dim
    child3 As New TreeViewItem

    child3.Name = "C"

    child3.Header = "C"

    node1.Items.Add(child3)


    tv.Items.Add(node1)

    End Sub

     

    Private Sub tv_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles tv.SelectedItemChanged

    Dim item As TreeViewItem = tv.SelectedItem

    Me.tbMessage.Text = item.Header

    End Sub

     

    End Class

     

     

    • Marked as answer by ddaoliver Thursday, November 5, 2009 6:18 PM
    Sunday, October 25, 2009 2:47 AM