Accessing the ParentItem and RootItem

4 posts, 0 answers
  1. Yu
    Yu avatar
    4 posts
    Member since:
    Sep 2012

    Posted 27 Sep 2013 Link to this post

    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!


  2. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1183 posts

    Posted 02 Oct 2013 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Yu
    Yu avatar
    4 posts
    Member since:
    Sep 2012

    Posted 06 Oct 2013 Link to this post

    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

  5. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 09 Oct 2013 Link to this post

    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 >>
Back to Top