This is a migrated thread and some comments may be shown as answers.
Treeview selection
4 Answers 32 Views
This is a migrated thread and some comments may be shown as answers.
Jake
Top achievements
Rank 1
Jake asked on 29 Aug 2011, 07:20 AM
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!

4 Answers, 1 is accepted

Sort by
0
Anne Lyon
Top achievements
Rank 1
answered on 31 Aug 2011, 11:54 AM
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?
0
Kiril Stanoev
Telerik team
answered on 31 Aug 2011, 04:47 PM
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 >>

0
Jake
Top achievements
Rank 1
answered on 02 Sep 2011, 08:00 AM
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. 
0
Jake
Top achievements
Rank 1
answered on 02 Sep 2011, 08:26 AM
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().
Asked by
Jake
Top achievements
Rank 1
Answers by
Anne Lyon
Top achievements
Rank 1
Kiril Stanoev
Telerik team
Jake
Top achievements
Rank 1
Share this question
or