Treeview selection

5 posts, 0 answers
  1. Jake
    Jake avatar
    4 posts
    Member since:
    May 2011

    Posted 29 Aug 2011 Link to this post

    Hey guys,

    I have a problem I'm trying to solve. I'm trying to programmatically select an item in the tree view. I'm doing something like this:

    public void selectCurrentNodeByRelease(Release sentObject)

            {

                foreach (RadTreeViewItem i in this.treeRelease.Items)

                {

                    if (i.Tag == sentObject)

                    {

                        i.ExpandAll();

                        i.IsSelected = true;

                    }

                }

            }



    however it doesn't seem to be a hard select. When I click on one of the tree view items it's highlighted yellow (default theme), but when I select it programmatically it's highlighted silver. If I try to get the currently selected tree view item after doing the above it will come back as null (I.E. trying RadTreeViewItem item = (RadTreeViewItem)this.treeRelease.SelectedItem;). It's also not throwing my SelectionChanged event. Am I doing this wrong? 

    Thanks!
  2. Anne Lyon
    Anne Lyon avatar
    87 posts
    Member since:
    Jan 2010

    Posted 31 Aug 2011 Link to this post

    I am trying to do exactly the same and experience the same problem.
    I have a Header, or a Tag for that matter, and I need to find the node in the tree that corresponds to this, select it and expand it.
    I find the node easily (by recursively traversing my tree) but 'foundNode.IsSelected= true' does not fire the Selected event and the tree's SelectedItem is null.
    What can we do?
  3. DevCraft banner
  4. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 31 Aug 2011 Link to this post

    Hi Jake,

    You need to focus the treeview after programatically selecting an item. Please take a look at the attached project for further reference and let me know if this approach suites you.

    @Anne - I was not able to reproduce the issue you describe. Please take a look at the attached project as well.

    Best wishes,
    Kiril Stanoev
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  5. Jake
    Jake avatar
    4 posts
    Member since:
    May 2011

    Posted 02 Sep 2011 Link to this post

    Hey Kiril,

    That didn't work for me. I looked at the example you sent as well and didn't see a RadTreeView in the project. Here's what I'm trying now:

    public void selectNodeByRelease(Release sentObject)
    {
    foreach (RadTreeViewItem i in this.treeRelease.Items)
    {
    if (i.Tag == sentObject)
    {
    i.ExpandAll();
    i.Focus();
    }
    }

    }


    The set focus didn't help, I tried setting it on the node and on the treeview itself. I also tried doing this.tree.selecteditem = i and that didn't work either. 
  6. Jake
    Jake avatar
    4 posts
    Member since:
    May 2011

    Posted 02 Sep 2011 Link to this post

    nevermind, I got it. I need to focus() before I set the isSelected bool. Thanks Kiril, never would have figured out that I had to use Focus().
Back to Top
DevCraft banner