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

Programmatic access to treeview items

25 Answers 456 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
hwsoderlund
Top achievements
Rank 1
hwsoderlund asked on 10 Feb 2009, 09:44 AM
I was going to post a reply in the following thread to bump it, but it seems to have been locked:
http://www.telerik.com/community/forums/silverlight/treeview/feature-request.aspx

Anyway, I just want to raise the issue again of easier access to individual items in the treeview, including the ones not yet generated. More to the point; I want to be able to expand the tree to specific nodes. Something like "RadTreeView.ExpandBranchToNode(TreeViewItem node) would be great.



25 Answers, 1 is accepted

Sort by
0
Tihomir Petkov
Telerik team
answered on 10 Feb 2009, 12:22 PM
Hello Henrik,

We will try to address your request for the coming Q1 release or SP1 at the latest.

Regards,
Tihomir Petkov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
hwsoderlund
Top achievements
Rank 1
answered on 30 Mar 2009, 11:56 AM
Hi, has there been any progress on this issue? /Henrik
0
Accepted
Bobi
Telerik team
answered on 30 Mar 2009, 01:05 PM
Hi hwsoderlund,

In SP1 we will add appropriate method for this functionality. The SP1 is due in two weeks. I hope this is acceptable for you.

Regards,
Boryana
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Jarred Froman
Top achievements
Rank 1
answered on 15 May 2009, 02:54 PM
I'm looking to accomplish this same task.  What are the methods and how are they used to accomplish this task in SP1?

Thanks,
Jarred Froman
0
Tihomir Petkov
Telerik team
answered on 18 May 2009, 10:59 AM
Hi Jarred,

Currently RadTreeView has an ExpandItemByPath method which, however, has some limitations. We are going to enhance the method to work in most if not all scenarios for the coming SP2 release which is due at the end of this week.

Sincerely yours,
Tihomir Petkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
hwsoderlund
Top achievements
Rank 1
answered on 18 May 2009, 11:26 AM
Perhaps you can explain a bit more about what we can expect from this method. At the moment ExpandItemByPath does not seem to work when using data binding. I'm assuming this issue will be addressed in SP2?
0
Tihomir Petkov
Telerik team
answered on 18 May 2009, 11:44 AM
Hello Henrik,

The enhancements that I mentioned in my previous post are regarding exactly the use of data binding. So, in the SP2 release you will be able to use the ExpandItemByPath method to drill down the source hierarchy no matter if the child items' containers have been generated or not e.g. expanding RadTreeView to a certain item in the constructor of your page.

All the best,
Tihomir Petkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
hwsoderlund
Top achievements
Rank 1
answered on 18 May 2009, 12:04 PM
Nice one. Thanks.
0
Accepted
Miroslav
Telerik team
answered on 19 May 2009, 10:19 AM
Hello Henrik & Jarred,

You can use the ExpandByPath & SelectByPath methods even now, you just need to dispatch them. Something like this:

treeView.ItemsSource = new object[] { GetTopOrganisation("A") };  
 
//This will not work:  
//ExpandItemAndSelect();   
 
//But this will work:  
this.Dispatcher.BeginInvoke(ExpandItemAndSelect);  
//The above is the same as this:  
//this.Dispatcher.BeginInvoke(() => ExpandItemAndSelect());  
 


private void ExpandItemAndSelect()  
{  
    var path = "Top Organisation A|Organisation 1.1 A|Organisation 2.1 A|Organisation 3.1 A|Person 3.1.1";  
    treeView.ExpandItemByPath(path, "|");  
    treeView.GetItemByPath(path, "|").IsSelected = true;  

It will also work with databound items.

You can have a look at the attached example,

Hopefully this is what you need,

Best wishes,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
hwsoderlund
Top achievements
Rank 1
answered on 19 May 2009, 12:23 PM
Cool, that should do the trick. What I would like to see improved though, is the possibility of specifying which property should be used when calculating the path. Having to use the actual text displayed in each node is a bit cumbersome. And what if the DataTemplate only contains images, and no text? It would be great if it was possible to say, for instance, that I want to use the OrganisationID as a path property, then I could just set the path to "12|735|917", and it would always work.
0
Jarred Froman
Top achievements
Rank 1
answered on 19 May 2009, 02:57 PM
I'll give that a try and see how it works out for us.  Also, is it possible to make the ContainerFromItemRecursive function work the same?  This would definitely make it a lot easier rather than having to build out the full path.

Thanks for the info and the sample!  :)  
Jarred Froman
0
Miroslav
Telerik team
answered on 20 May 2009, 06:47 AM
Hello Jarred & Henrik,

Yes, the ToString() may now work in all cases and there surely are better uses for it. This is one of the improvements we are thinking about.

As for the ContainerFromItemRecursive, I am afraid that it will really slow down the TreeView at times and I am a bit unsure whether it is a good idea to force the creation of items. The reason for this is that with the path you need only to generate the container of the "branch" of the TreeView that you need. On the other hand to search for a container, you need to start generating containers until you find it (breadth-first or depth-first). At worst, this means generating the containers for all the items just to find one.

Greetings,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Miroslav
Telerik team
answered on 20 May 2009, 07:12 AM
Hi Jarred,

Sorry if a my last reply sounded a bit dismissive. I am thinking now of the reasons why you may need the container.

- You want to set a property. Then you can always use the ItemContainerStyle. Even attached properties can be set this way.

- You want to bind a property. Then you can use the not-so-well-promoted-but-still-great feature of all our items controls: the ContainerBinding. You can attach a ContainerBindingCollection to the root element of the ItemTemplate, it will contain bindings that will be applied to the container when it is generated. This way you will not have to manually change things like IsEnabled, IsSelected, etc.

Hopefully this will be helpful :)

Best wishes,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
hwsoderlund
Top achievements
Rank 1
answered on 20 May 2009, 08:07 AM
The ContainerBinding stuff looks like a great feature. I wish I had known about it sooner. :) But I'm having a problem. I've done a simple test and successfully managed to bind the IsEnabled, Header and IsSelected properties of the TreeViewItem. But what I really need for my application is to bind the IsChecked property. And that doesn't work. Any ideas?

<UserControl x:Class="TelerikTestProject.Page" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" 
             xmlns:telerikinput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input" 
             xmlns:teleriknav="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             Width="400" 
             Height="300"
    <UserControl.Resources> 
        <telerik:ContainerBindingCollection x:Key="TreeItemContainerBindings"
             
            <telerik:ContainerBinding PropertyName="IsEnabled" 
                                      Binding="{Binding IsSelected}" /> 
             
            <telerik:ContainerBinding PropertyName="IsSelected" 
                                      Binding="{Binding IsSelected}" /> 
             
            <telerik:ContainerBinding PropertyName="Header" 
                                      Binding="{Binding Name}" /> 
             
            <!--Does not work--> 
            <telerik:ContainerBinding PropertyName="IsChecked" 
                                      Binding="{Binding IsSelected}" /> 
             
        </telerik:ContainerBindingCollection> 
    </UserControl.Resources> 
     
    <Grid x:Name="LayoutRoot" 
          Background="White"
 
        <Grid.RowDefinitions> 
            <RowDefinition Height="Auto"></RowDefinition> 
            <RowDefinition Height="Auto"></RowDefinition> 
        </Grid.RowDefinitions> 
 
        <teleriknav:RadTreeView x:Name="RadTreeView1" 
                                IsOptionElementsEnabled="True" 
                                ItemsOptionListType="CheckList" 
                                SelectionMode="Multiple" 
                                Grid.Row="1"
            <teleriknav:RadTreeView.ItemTemplate> 
                <telerik:HierarchicalDataTemplate ItemsSource="{Binding SubTopics}" 
                                                  telerik:ContainerBinding.ContainerBindings="{StaticResource TreeItemContainerBindings}"
                </telerik:HierarchicalDataTemplate> 
            </teleriknav:RadTreeView.ItemTemplate> 
        </teleriknav:RadTreeView> 
    </Grid> 
</UserControl> 


The class I'm binding to looks like this:
public class HelpTopic  
    public string Name { getset; } 
     
    public ObservableCollection<HelpTopic> SubTopics { getset; } 
 
    public  bool? IsSelected { getset; } 
 
    public HelpTopic() 
    { 
    } 




0
Valentin.Stoychev
Telerik team
answered on 22 May 2009, 11:50 AM
Hello Henrik,

We don't have IsChecked property :). Use the CheckState property and use a converter to bind it.


Greetings,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
hwsoderlund
Top achievements
Rank 1
answered on 23 May 2009, 10:57 PM
Ha ha, you're right of course. Silly me... Thanks. :)
0
Jie
Top achievements
Rank 1
answered on 08 Jun 2009, 11:27 PM
Deleted...wrong post.
0
James
Top achievements
Rank 1
answered on 08 Jul 2009, 07:32 AM
Miroslav

I have my tree view's LoadOnDemmand enabled and I tried to load child nodes of a specific node (child not loaded yet) programmatically. So I tried your code above to find the node I want to expand (load children) like below code:

 private void ExpandItemAndSelect(string path)
    {
        treeView.ExpandItemByPath(path, "|");
        RadTreeViewItem item = treeView.GetItemByPath(path, "|"); // node to expand
        item.IsExpanded = true;      // expand it
    }

I got an error message in my IE left bottom corner and it seems an error from the treeview control itself:

Sys.InvalidOpreationException: ManagedRuntimeError error #4004 in control "Xmal1": System.IndexOutOfRangeException: Index was outside the bounds of the array. at  Telerik.Windows.Controls.RadTreeView.RadTreeView_ItemPrepared......

was i doing something wrong?

0
James
Top achievements
Rank 1
answered on 09 Jul 2009, 02:41 AM
can anyone help?
0
Miroslav
Telerik team
answered on 09 Jul 2009, 02:06 PM
Hello James,

Sorry for the delayed reply,

I could not figure out the cause of the exception.

Could you provide the full stack trace of the exception?
Also, are you using SL2 or SL3?
Do you see the exception in the sample project as well?

In the Q2 release the GetItemByPath and ExpandItemByPath work in a very similar way, with just one difference: ExpandItemByPath works asynchronously, while GetItemByPath forces layout updates and tries to complete synchronously. Still, even if some of the items are not loaded they are not expected to throw an exception.

Thank you for helping us get to this issue.

Best wishes,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
James
Top achievements
Rank 1
answered on 16 Jul 2009, 01:53 AM
hi Miroslav
I'm using Silverlight 2 and Telerik control build version is 2009.1.526.1020. This error only occurs when the node is not expanded. Here is the stack trace of the error I got in my browser:

Error: Sys.InvalidOperationException: ManagedRuntimeError error #4004 in control 'Xaml1': System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Telerik.Windows.Controls.RadTreeView.RadTreeView_ItemPrepared(Object sender, RadTreeViewItemPreparedEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at Telerik.Windows.Controls.RadTreeView.OnItemPrepared(RadTreeViewItemPreparedEventArgs e)
   at Telerik.Windows.Controls.RadTreeView.PrepareTreeViewItemForItem(DependencyObject element, Object item, ItemsControl owner)
   at Telerik.Windows.Controls.RadTreeViewItem.PrepareContainerForItemOverride(DependencyObject element, Object item)
   at System.Windows.Controls.ItemsControl.UpdateContainerForItem(Int32 index)
   at System.Windows.Controls.ItemsControl.RecreateVisualChildren()
   at System.Windows.Controls.ItemsControl.RecreateVisualChildren(IntPtr unmanagedObj)
Source File: http://localhost/myapp/ScriptResource.axd?d=SLdRgtkPsj-3ZQlv_RyuS6aL18JTvjqatIbzRBh72zwTz-vN-Jj8nJTrzI9ghi9-VOZ9j_5U7tFcPksYUTx1pw2&t=ffffffffafbd6209
Line: 461

update: I tried the latest build:2009_2_701 same problem
0
Bobi
Telerik team
answered on 20 Jul 2009, 03:11 PM
Hello James,

We were unable to reproduce this issue.
Can you please send us a sample project in order to fix it.
We need some more information about the data source you use and the way you populate RadTreeView items.
May be you try to bind RadTreeView item with out of range data.

However ExpandItemByPath works asynchronously, while GetItemByPath forces layout updates and tries to complete synchronously. Still, even if some of the items are not loaded they are not expected to throw an exception.

Greetings,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Mark
Top achievements
Rank 1
answered on 28 Jul 2009, 02:46 PM
I have followed the insturctions listed in this thread and everything is working for small expantion paths (below 57). However I am dealing with a dataset that has paths above 100 nodes. Everytime I call ExpandItemByPath with a path above 57 node I get the following exception in the UnhandledException event handler method:

Exception of type 'System.InvalidOperationException' was thrown.

   at Telerik.Windows.RadRoutedEventHelper.BuildRoute(DependencyObject element, EventRoute route, RadRoutedEventArgs args)
   at Telerik.Windows.RadRoutedEventHelper.RaiseEvent(DependencyObject element, RadRoutedEventArgs args)
   at Telerik.Windows.DependencyObjectExtensions.RaiseEvent(DependencyObject element, RadRoutedEventArgs e)
   at Telerik.Windows.Controls.RadTreeViewItem.OnPreviewSelected(RadRoutedEventArgs e)
   at Telerik.Windows.Controls.RadTreeViewItem.IsSelectedPropertyConstrainValue(DependencyObject d, Object newValue)
   at Telerik.Windows.PropertyMetadata.CoerceValue(DependencyObject d, DependencyPropertyChangedEventArgs e, CoerceValueCallback callback)
   at Telerik.Windows.PropertyMetadata.<>c__DisplayClass1.<Create>b__0(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object newValue, Object oldValue)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet, Boolean isSetByStyle, Boolean isSetByBuiltInStyle, PropertyInvalidationReason reason)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Telerik.Windows.Controls.RadTreeViewItem.set_IsSelected(Boolean value)
   at Telerik.Windows.Controls.RadTreeView.SelectionChanger_SelectionChanged(Object sender, SelectionChangedEventArgs e)
   at Telerik.Windows.Controls.SelectionChanger`1.InvokeSelectionChangedEvent()
   at Telerik.Windows.Controls.SelectionChanger`1.End()
   at Telerik.Windows.Controls.RadTreeView.SetSelectedItem(RadTreeViewItem itemContainer)
   at Telerik.Windows.Controls.RadTreeView.HandleItemSelectionFromUI(RadTreeViewItem itemToSelect)
   at Telerik.Windows.Controls.RadTreeView.HandleDownArrowKey(KeyEventArgs e)
   at Telerik.Windows.Controls.RadTreeView.HandleKeyDown(KeyEventArgs e)
   at Telerik.Windows.Controls.RadTreeViewItem.OnKeyDown(KeyEventArgs e)
   at System.Windows.Controls.Control.OnKeyDown(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

Any help on this issue would be apprieciated.
0
Raj
Top achievements
Rank 1
answered on 30 Jul 2009, 01:50 PM
It's amazing to me how something as important as ContainerBinding is casually mentioned in a forum thread. Even though you're forum responses are very prompt (one reason I continue to use your controls), there needs to be a better way to get this type of information out. Maybe more detailed samples or deeper documentation.
0
Bobi
Telerik team
answered on 31 Jul 2009, 09:00 AM
Hi Raj,

For now RadTreeView does not support the scenario you mentioned:" ExpandItemByPath with a path above 57 node". This is because of the limitation of visual elements count in the Silverlight platform.


Sincerely yours,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
TreeView
Asked by
hwsoderlund
Top achievements
Rank 1
Answers by
Tihomir Petkov
Telerik team
hwsoderlund
Top achievements
Rank 1
Bobi
Telerik team
Jarred Froman
Top achievements
Rank 1
Miroslav
Telerik team
Valentin.Stoychev
Telerik team
Jie
Top achievements
Rank 1
James
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Raj
Top achievements
Rank 1
Share this question
or