locked
How to get TreeViewItem from the source item object RRS feed

  • Question

  • I have a TreeView bound to a view model. Now I’m trying to get the corresponding TreeViewItem for any given object from the view model.

    I can find examples on how to get the SelectedItem, but not how to get it for any arbitrary item.

     

    I have tried

    myTree.ItemContainerGenerator.ContainerFromItem(myNode);

     

    But it always returns null. (I know the node exist in the data and is visible in the tree)

     

    Any ideas ?

     

    Wednesday, July 21, 2010 4:35 PM

Answers

  • Hi Jkristia,

    Just like noorbakhsh said, you can add a new boolean property to your data model, and bind that to the IsSelected property of the TreeViewItem. Then both tree will be sync. Also, you can add other properties like "IsExpanded" when necessary.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by jkristia Friday, July 23, 2010 1:40 PM
    Friday, July 23, 2010 7:51 AM

All replies

  • Could you explain why you want retreive TreeViewItem or what is it you want to achieve? Maybe there is another solution that works around this.

    Thanks

    Peter

    Wednesday, July 21, 2010 5:17 PM
  • jkristia,

    ItemContainerGenerator only returns items that have been generated Visually. If the item is not in view (and I mean visible on the screen), it might return a null since the item could be getting recycled.

    When dealing with MVVM, you should concentrate on items in ViewModel and manipulate those instead of trying to access them from the View. This said, there are multiple instances when I need to do what I just suggested against, so maybe if you explain what you are doing in a greater detail will help others to give you hints to as what is the best way to approach this as Peter has suggested.

    Why do you need to access each TreeViewItem from the View?


    noorbakhsh حميد نوربخش
    Wednesday, July 21, 2010 8:38 PM
  • What I'm trying to do is to synchronize 2 (or more) trees. Both trees are hooked up to the same view model (but one tree shows just a subset of the nodes !!), and when an item is selected in one tree I need to select the same node (or the first visible ancestor node) in the other tree.

    After searching I found a 'hacky' way of selecting a TreeViewItem in the tree using TreeViewItem's Select method (by reflection since it is private).

    Any other suggestions how I can do this?

    Oh, and for the ContainerFromItem. It does work, but it needs to be called on the TreeViewItem itself when the node is a child of a node, and on the TreeView when the node is at the root level. (it actually makes sense)

    Wednesday, July 21, 2010 8:45 PM
  • Hi Jkristia,

    Just like noorbakhsh said, you can add a new boolean property to your data model, and bind that to the IsSelected property of the TreeViewItem. Then both tree will be sync. Also, you can add other properties like "IsExpanded" when necessary.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by jkristia Friday, July 23, 2010 1:40 PM
    Friday, July 23, 2010 7:51 AM
  • You are absolutely right - I alreayd had it hooked up to the IsSelected property, but what I was missing was the OnPropertyChanged notificaiton. Once added, the sync works as expected wihtout any hack.

    Thanks

     

    Friday, July 23, 2010 1:39 PM