We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get the full path of selected TreeViewItemAdv of the TreeViewAdv?

Platform: WPF |
Control: TreeViewAdv |
.NET Framework: 4

At present there is no direct option retrieve the path of TreeViewItemAdv. It is possible to retrieve the path of selected TreeViewItemAdv by handling SelectedItemChanged event and using VisualTreeHelper.

 

The following code demonstrates the same.

 

XAML

  <syncfusion:TreeViewAdv Name="treeViewAdv" >
            <syncfusion:TreeViewItemAdv Name="treeViewItemAdv" Header="Marital Status">
                <syncfusion:TreeViewItemAdv Header="Single">
                    <syncfusion:TreeViewItemAdv Header="Student"/>
                    <syncfusion:TreeViewItemAdv Header="Exployee"/>
                    <syncfusion:TreeViewItemAdv Header="Businessman"/>
                </syncfusion:TreeViewItemAdv>
                <syncfusion:TreeViewItemAdv Header="Married"/>
                <syncfusion:TreeViewItemAdv Header="Married with Children"/>
            </syncfusion:TreeViewItemAdv>
            <syncfusion:TreeViewItemAdv Header="Baby Vaccines">
                <syncfusion:TreeViewItemAdv Header="Hepatitis B"/>
                <syncfusion:TreeViewItemAdv Header="Tetanus"/>
                <syncfusion:TreeViewItemAdv Header="Polio"/>
                <syncfusion:TreeViewItemAdv Header="Measles"/>
            </syncfusion:TreeViewItemAdv>
            <syncfusion:TreeViewItemAdv Header="Country Information">
                <syncfusion:TreeViewItemAdv Header="Canada"/>
                <syncfusion:TreeViewItemAdv Header="France"/>
                <syncfusion:TreeViewItemAdv Header="Germany"/>
                <syncfusion:TreeViewItemAdv Header="UK"/>
                <syncfusion:TreeViewItemAdv Header="USA"/>
            </syncfusion:TreeViewItemAdv>
   </syncfusion:TreeViewAdv>

 

C#

        public MainWindow()
        {
            InitializeComponent();
            treeViewAdv.SelectedItemChanged += TreeViewAdv_SelectedItemChanged;
        }
 
        /// <summary>
        /// Invoked when the SelectedItem changed
        /// </summary>
        private void TreeViewAdv_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            MessageBox.Show("Selected Item Path: " + GetFullPath((treeViewAdv.SelectedTreeItem as TreeViewItemAdv)));
        }
 
        /// <summary>
        /// To get the full path of the TreeViewItemAdv
        /// </summary>
        public string GetFullPath(TreeViewItemAdv node)
        {
            if (node == null)
                throw new ArgumentNullException();
 
            var result = Convert.ToString(node.Header);
 
            for (var i = GetParentItem(node); i != null; i = GetParentItem(i))
                result = i.Header + "\\" + result;
 
            return result;
        }
 
        /// <summary>
        /// To get the parent of the item
        /// </summary>
        static TreeViewItemAdv GetParentItem(TreeViewItemAdv item)
        {
            for (var i = VisualTreeHelper.GetParent(item); i != null; i = VisualTreeHelper.GetParent(i))
                if (i is TreeViewItemAdv)
                    return (TreeViewItemAdv)i;
 
            return null;
        }

 

Path reviewed

 

Sample: TreeViewAdvSample

 

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile