This is a migrated thread and some comments may be shown as answers.

'Selected' not fired when node not visible in tree

3 Answers 172 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Yu
Top achievements
Rank 1
Yu asked on 13 Jul 2011, 06:57 AM
Hi,

I'd like to clarify whether this is an expected behavior.

I'm creating a windows explorer-like control where the RadTreeView is used on the left side pane (l-pane) for navigation and a RadGridView is used for right side detail pane (r-pane).

When the node in the tree is selected, its children are displayed in the r-pane. When a row in r-pane is double-clicked, l-pane will navigate to the node corresponding to that row (if there's any child under it, otherwise there will be other behaviors), and the children of that node populate the r-pane.

I'm using MVVM. Each node in the tree and row in the grid view is bound to a view model. The same view model is used for both the node (if it appears in the tree) and the row element (if it appears in the grid view). And I'm controlling the IsExpand and IsSelected status of the tree node through the view model's corresponding properties with the same name (two way binding). All very standard.

I also have a view model for the whole explorer. In here I hooked a few ICommand's to the various events of both the tree view and the grid view. For example, I created an ICommand for the Selected event of the tree, so that when the user select a certain node, I'll update the r-pane's bound collection. Another ICommand responses to the grid view's MouseDoubleClick event so when the user double click the row in grid view, the entry view model's IsSelected and it's parent's IsExpanded are set to true. And here's where the problem occurs.

Let's look at those nodes previously not shown in the tree yet (i.e. their parents are not expanded yet).

After the above step the tree successfully expands and shows the node in question as selected. After that what I expected is that the 'Selected' event of the tree view is fired, my ICommand for 'Selected' is executed, and my r-pane is populated with the node's children. But the 'Selected' event is never fired!

The reason i guess is that, since the node's parent was not expanded yet, the node was not visible/loaded in the tree when my ICommand for r-pane's MouseDoubleClick was executed. And what's more, even after I set its IsSelected property to true programmatically, somehow the event routing just stopped there.

One interesting thing I found is that, if I manually expand the parent in the tree, and make the node visible, then go back to r-pane and double click on the row, the 'Selected' event is fired now! I can see the r-pane is repopulated with the node's children.

 It seems that after I expand the parent once, even if I immediately un-expand it afterwords, I somehow trigger the node to be loaded in the tree. And now it's legitimate to wake up the 'Selected' event of the tree view control!

Here's the Link to the demo project on skydrive. (RadTreeViewProblem.rar)

My question here: is this expected? Of course I can set all the nodes in the tree to be Expanded when it is loaded to make this work. But that's certainly not what I want.

Any input on this is appreciated!

3 Answers, 1 is accepted

Sort by
0
Yu
Top achievements
Rank 1
answered on 13 Jul 2011, 09:12 AM
Here's the Link to the demo project on skydrive. (RadTreeViewProblem.rar)

1. Compile and run
2. double click on Division A in right pane
- Observe:
left pane shows Division A selected but still not expanded; right pane shows the content of Division A
3. double click on Team I in right pane
- Observe:
left pane shows Team I under Division A League A selected. Its parent expanded.
Right pane still shows the content of Division A with Team I selected.
4. double click on Team II under Division A League A in right pane
- Observe:
left pane shows Team II selected
right Pane shows content of Team II

0
Accepted
Petar Mladenov
Telerik team
answered on 18 Jul 2011, 09:04 AM
Hi Yu,

The Selected event is inherited from the Selector class, but its better to use the SelectionChanged event in many cases. Could you please try this out:
<i:Interaction.Triggers>
               <i:EventTrigger EventName="SelectionChanged">
                   <i:InvokeCommandAction Command="{Binding TreeNodeSelectionCommand}"
                           CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType={x:Type telerik:RadTreeView}}}" />
               </i:EventTrigger>
           </i:Interaction.Triggers>
and let us know if the behavior what you need is achieved?


Greetings,
Petar Mladenov
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Yu
Top achievements
Rank 1
answered on 20 Jul 2011, 07:46 AM
Yup. Tried and worked like a charm! Thanks a lot!
I'll stick to the SelectionChanged event from now on.
Tags
TreeView
Asked by
Yu
Top achievements
Rank 1
Answers by
Yu
Top achievements
Rank 1
Petar Mladenov
Telerik team
Share this question
or