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

Accessing the ParentItem and RootItem

3 Answers 207 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Yu
Top achievements
Rank 1
Yu asked on 27 Sep 2013, 05:37 PM

In WPF Help System(2013 Q2)



Controls-->RadTreeView-->How To-->Get Previous, Next, Parent and Sibling Node of a Specific TreeView Item-->Accessing the ParentItem and RootItem



ObservableCollection<Object> selectedItems = treeView.SelectedItems;

RadTreeViewItem item = selectedItems[ 0 ] as RadTreeViewItem;



Maybe not cast successfully. Should be?



RadTreeViewItem item = treeView.SelectedContainer ;



In RadTreeView class(Pay a attention to both *item and *contrainer type):

public RadTreeViewItem SelectedContainer { get; private set; }

public Object SelectedItem { get; set; }



But in RadTreeView class:

public Object Item { get; internal set; }

public RadTreeViewItem NextItem { get; }

public RadTreeViewItem ParentItem { get; internal set; }

public RadTreeViewItem PreviousItem { get; }



I think it should be:

public Object Item { get; internal set; }

public Object NextItem { get; }

public Object ParentItem { get; internal set; }

public Object PreviousItem { get; }



public RadTreeViewItem NextContainer { get; }

public RadTreeViewItem ParentContainer { get; internal set; }

public RadTreeViewItem PreviousContainer { get; }



In both RadTreeView and RadTreeViewItem, Item and its Container should be clearly different.

Thanks!


3 Answers, 1 is accepted

Sort by
0
Pavel R. Pavlov
Telerik team
answered on 02 Oct 2013, 01:39 PM
Hi Yu,

As far as I understand you refer to this article and you are concerned about the following cast:

private void radTreeView_SelectionChanged( object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e )
{
    // Get a reference to the treeview
    Telerik.Windows.Controls.RadTreeView treeView = sender as Telerik.Windows.Controls.RadTreeView;
    // Get the currently selected items
    ObservableCollection<Object> selectedItems = treeView.SelectedItems;
    RadTreeViewItem item = selectedItems[ 0 ] as RadTreeViewItem;
    // Get the previous item and the previous sibling item
    RadTreeViewItem previousItem = item.PreviousItem;
    RadTreeViewItem previousSiblingItem = item.PreviousSiblingItem;
 
    // Get the next item and the next sibling item
    RadTreeViewItem nextItem = item.NextItem;
    RadTreeViewItem nextSiblingItem = item.NextSiblingItem;
 
    // Get the parent item and the root item
    RadTreeViewItem parentItem = item.ParentItem;
    RadTreeViewItem rootItem = item.RootItem;
}
Please note that the RadTreeView control in the referenced article is XAML defined, hence the SelectedItems collection consists of RadTreeViewItems, only. Hence the cast will always be successful.

However, if you use data bindings you actually use the RadTreeView control in different scenario and the SelectedItems collection will hold your business items.

Regarding your last sentence - I am not sure that I understand your point. Could you please clarify what you have in mind when you say: "In both RadTreeView and RadTreeViewItem, Item and its Container should be clearly different."?

Regards,
Pavel R. Pavlov
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Yu
Top achievements
Rank 1
answered on 06 Oct 2013, 02:08 PM

Regarding your last sentence - I am not sure that I understand your point. Could you please clarify what you have in mind when you say: "In both RadTreeView and RadTreeViewItem, Item and its Container should be clearly different."?



I meam the type of the  properties of RadTreeView/RadTreeViewItem may not be clear.



The type of RadTreeView.SelectedItem is object

The type of RadTreeView.SelectedContainer is RadTreeViewItem

The type of RadTreeViewItem.Item is object

The type of RadTreeViewItem.ParentItem is RadTreeViewItem

The type of RadTreeViewItem.NextItem is RadTreeViewItem

The type of RadTreeViewItem.PreviousItem is RadTreeViewItem



I think:



the type of RadTreeView*.*Item should be object

the type of RadTreeView*.*Container should be RadTreeViewItem

0
Hristo
Telerik team
answered on 09 Oct 2013, 02:06 PM
Hello Yu,

You are right about the inconsistent naming of Previous/Next/ParentItem properties. However we could not change them because it would be a breaking change to the existing applications using RadTreeView.

Regards,
Hristo
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
TreeView
Asked by
Yu
Top achievements
Rank 1
Answers by
Pavel R. Pavlov
Telerik team
Yu
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or