How to get Parent Item

4 posts, 1 answers
  1. Vit100
    Vit100 avatar
    66 posts
    Member since:
    Oct 2012

    Posted 30 Aug 2010 Link to this post

    I have DataModel 
    Country -> List<City>
    and this is shown in RadTreeView with load on demand.

    I need to know at any time full path to selection in TreeView ( I call different function depending on Selectiion in Tree).
    How to achive this?

    If I take example from documentation :
    it gives error.
    I need to know at any time what is full path to selection in TreeView. How to achive this.
    Sample project attached.
    Pls help.
               <DataTemplate x:Key="PopulationTemplate">
                   <StackPanel Orientation="Horizontal">
                       <TextBlock Text="{Binding Name}"/>
                       <TextBlock Text=": "/>
                       <TextBlock Text="{Binding Count}"/>
               <telerik:HierarchicalDataTemplate x:Key="CountriesTemplate"
                                                 ItemsSource="{Binding Cities}"
                                                 ItemTemplate="{StaticResource PopulationTemplate}">
                   <TextBlock Text="{Binding Name}"/>
           <telerik:RadTreeView ItemsSource="{Binding Countries}" IsLoadOnDemandEnabled="True"
                               ItemTemplate="{StaticResource CountriesTemplate}"
                                SelectedItem="{Binding SelectedItem, Mode=TwoWay}"

    public partial class MainPage : UserControl
            Model.ViewModel vm;
            public MainPage()
                vm = new Model.ViewModel();
                this.DataContext = vm;
            private void RadTreeView_LoadOnDemand(object sender, Telerik.Windows.RadRoutedEventArgs e)
                RadTreeViewItem currentItem = e.OriginalSource as RadTreeViewItem;
                  Country country = currentItem.Item as Country;
                if (country != null)
                City city = currentItem.Item as City;
                if (city != null)
                    currentItem.IsLoadOnDemandEnabled = false;
                    var c = currentItem.ParentItem.Item as Country;
                    vm.LoadExtraData(c, city);
            private void RadTreeView_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
                RadTreeViewItem item = e.PreparedItem as RadTreeViewItem;
                if (item != null && item.Item is City)
                    item.IsLoadOnDemandEnabled = false;
            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;
                //Here always null!!!!!!!!!!!!!!!!!!!!!!!!!!!! because selectedItem is not RadTreeViewItem - it is Country or City
                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;

    public class ViewModel : ModelBase
            public ObservableCollection<Country> Countries { get; set; }
            public ViewModel()
                Countries = new ObservableCollection<Country>();
            private Country _SelectedCountry;
            public Country SelectedCountry
                get { return _SelectedCountry; }
                set { _SelectedCountry = value; RaisePropertyChanged("SelectedCountry"); }
            private City _SelectedCity;
            public City SelectedCity
                get { return _SelectedCity; }
                set { _SelectedCity = value; RaisePropertyChanged("SelectedCity"); }
            private object _SelectedItem;
            public object SelectedItem
                get { return _SelectedItem; }
                set { _SelectedItem = value; RaisePropertyChanged("SelectedItem"); }
            public void LoadCountries()
                Countries.Add(new Country() { Name = "US" });
                Countries.Add(new Country() { Name = "Canada" });
            public void LoadCities(Country country)
                if (country.Name == "US")
                    country.Cities.Add(new City() { Name = "NY", Count = 1 });
                    country.Cities.Add(new City() { Name = "Atlanta", Count = 2 });
                if (country.Name == "Canada")
                    country.Cities.Add(new City() { Name = "Toronto", Count = 3 });
                    country.Cities.Add(new City() { Name = "Vancouver", Count = 4 });
           //here I call method that takes only country - means root item selected in the tree.
            public void LoadExtraData(Country country)
                //call to WCF with parameter country
                SelectedCountry = country;
            // here  I need full path - country and city..
            public void LoadExtraData(Country country, City city)
                //call to WCF with parameters country and city
                SelectedCountry = country;
                SelectedCity = city;
  2. Vit100
    Vit100 avatar
    66 posts
    Member since:
    Oct 2012

    Posted 31 Aug 2010 Link to this post

    I use code below in SelectionChanged event or RadTreeView
    RadTreeView treeView = sender as Telerik.Windows.Controls.RadTreeView;
    RadTreeViewItem p = treeView.ContainerFromItemRecursive(treeView.SelectedItem);
    and check type of item later and if it is child type I use

    p.Item.ParentItem.Item to get parent..

    Is this right way?
  3. Answer
    Hristo avatar
    352 posts

    Posted 01 Sep 2010 Link to this post

    Hello Vit100,

    You can use the FullPath property of RadTreeViewItem. Also you can register an event handler for the Selected event of the RadTreeView and retrieve the selected element.
    In order to receive meaningful string representation of selected element path you can do the following:
    1. Override the ToString() method of the business object you are binding to the treeview;
    2. Set the TextSearch.TextPath attached property of RadTreeView to property where the string will come from.

    Also you should use the SelectedContainer (returns RadTreeViewItem that holds appropriate business item) property instead SelectedItem.

    Last, I'm attaching a sample project demonstrating how to implement the suggested solution.

    Hrsito Milyakov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  4. Vit100
    Vit100 avatar
    66 posts
    Member since:
    Oct 2012

    Posted 01 Sep 2010 Link to this post

    thanks - yes,




    p = treeView.SelectedContainer


    is an answer..
Back to Top