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

Return all Items In Treeview

3 Answers 171 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Heavy
Top achievements
Rank 1
Heavy asked on 22 May 2009, 05:27 PM
Is there an easy way to return all the times in the treeview in one statement?  I have a treeview that I manually load through code that is 5-6 levels deep with checkboxes.  I then need to go through every item in the Treeview and check if it is checked or not and update the database.  It doesn't matter what order it goes in.  Right now, i am looping through each level then looping through the children and it is very slow.  Is there a more efficient way of looping through all items and getting the checkstate?

 

foreach (RadTreeViewItem item in treeColumns.Items)

 

{

 

DataAccess.ctx.ExecuteCommand("Update WorkFlow Set SortOrder=" + lOrder.ToString() + " Where ItemID='" + new Guid(item.Tag.ToString()) + "'");

 

RecursiveItems(item);

lOrder++;

}

 

private

 

void RecursiveItems(RadTreeViewItem treeitem)

 

{

 

int lOrder = 1;

 

 

foreach (RadTreeViewItem item in treeitem.Items)

 

{

 

DataAccess.ctx.ExecuteCommand("Update WorkFlow Set SortOrder=" + lOrder.ToString() + " Where ItemID='" + new Guid(item.Tag.ToString()) + "'");

 

 

if (item.Items.Count > 0)

 

{

RecursiveItems(item);

}

lOrder++;

}

 

DataAccess.ctx.SubmitChanges();

 

}

3 Answers, 1 is accepted

Sort by
0
Miroslav
Telerik team
answered on 25 May 2009, 02:42 PM
Hi Heavy,

There is no direct way to enumerate all the items of a TreeView.

If you are binding the TreeView, may I suggest using style bindings to update the models.

You can assign an ItemContainerStyle to the TreeView, like so

<Style TargetType="telerik:RadTreeViewItem">
    <Setter Property="CheckedState" Value="{Binding MyPropertyName, Mode=TwoWay, Converter={StaticResource AConverterIfNeeded}}" />
</Style>


Then it will automatically update the property when it changes.

If you are creating the TreeViewItems in code, you will have to enumerate them. (Or create bindings in code).

Hopefully this will work for you,

All the best,
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
Heavy
Top achievements
Rank 1
answered on 27 May 2009, 11:19 PM
Miroslav,
Thanks for the suggestion. I would prefer to directly bind as you pointed out.  Let me ask you how I would do this.  I have one table that holds all the times in the treeview.  Each item has an ItemID and a ParentItemID.  So it is easy to see how one item is a child of another, while the root nodes have no parents.  Right now, I manually loading them.  Because the number of levels are unknown at runtime and can always change, is there an easy way to load this and using a HierarcticalDataTemplate, or would I need one for every level?  Could it figure out the relationships itself?  Again, I use one table that joins to itself to build the tree and could be x number of levels at any node.

0
Miroslav
Telerik team
answered on 29 May 2009, 01:34 PM
Hello Heavy,

Sorry for the delayed reply!

The hierarchical binding in WPF (and Silverlight) works by getting the children for each parent. So each item needs to have an enumerable property (Enumerable, List or Collection) with all its children.

if the property us called ChildItems, then you can create a template like so:

<HierarchicalDataTemplate ItemsSource={Binding ChildItems}>
    <TextBlock Text={Binding PropertyToGetTitleFrom} />
</HierarchicalDataTemplate>

When you assign this as ItemTemplate to the TreeView it will automatically be inherited for all its items so there is no need to recursively set it again to manage different depth.

The thing here will be to build the children collections of your items. It may involve manually creating the children collection for each item, but it should be straightforward with the ParentID relation.  A bonus here is that if you use an ObservableCollection instead of List or Collection, it will notify the treeView whenever it changes. so that you will not have to sync the contents of the collection and the TreeView items manually if they change after the TreeView has loaded.

If you plan to have hundreds of items in the TreeView, you may want to use the LoadOnDemand which will allow you the children of an item only when they are expanded (made visible).

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.
Tags
TreeView
Asked by
Heavy
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
Heavy
Top achievements
Rank 1
Share this question
or