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

GetItemByPath returns null when tree is not visible.

4 Answers 145 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Scott Michetti
Top achievements
Rank 1
Scott Michetti asked on 03 Aug 2017, 02:56 PM

I have a Telerik Treeview on a Telerik Docking RadPane. If the pane is unpinned, so the user no longer can see the tree, a call to GetItemByPath returns null.

I give the user an option to pin the pane, and navigate the tree, or unpin the pane, and click "Next" and "Previous" buttons to navigate the tree. If the pane is pinned and the tree is visible, GetItemByPath returns the RadTreeViewItem. If the tree is hidden, GetItemByPath returns null.

If I expand all nodes on the tree first, GetItemByPath returns the RadTreeViewItem even when the tree is hidden. I have IsVirtualizing set to false.

Is there a way to set the SelectedItem using GetItemByPath when the tree is not visible? Or is there another call I can make?

Thanks,

Scott

4 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 08 Aug 2017, 11:01 AM
Hi Scott,

Can you please add more info on the unloading of the RadTreeView ? Is the visibility that is causing the issue or the unloading from visual tree - for example when unloaded, does the RadTreeView has any DataContext / ItemsSource bound correctly ? 

On the other hand, selecting an item can be split into 2 options - selecting a container (RadTreeViewItem) / selecting (marking) a dataitem. If you know the exact dataitem that needs its container selected or you have a search / iteration method that can give you this dataitem, then you can set property in the dataitem class - for example Selected = True. Then rest of the work is to bind the IsSelected of the RadTreeViewItem to the Selected of the DataItem via Style setter. Is this an option for you ?

Regards,
Petar Mladenov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Scott Michetti
Top achievements
Rank 1
answered on 08 Aug 2017, 02:52 PM

Thank you for the reply Petar. Here is my XAML and code. I included screenshots to show you what is going on.          

private void LoadButton_Click(object sender, RoutedEventArgs e)
        {

            radTreeView.ItemsSource = null;
            radTreeView.Items.Clear();

            List<RadTreeViewItem> _radTreeViewItems = new List<RadTreeViewItem>();

            RadTreeViewItem radTreeViewItem = new RadTreeViewItem();

            radTreeViewItem.Header = "TopLevel";

            RadTreeViewItem radTreeViewItemChild = new RadTreeViewItem();
            radTreeViewItemChild.Header = "Child1";
            radTreeViewItem.Items.Add(radTreeViewItemChild);

            radTreeViewItemChild = new RadTreeViewItem();
            radTreeViewItemChild.Header = "Child2";
            radTreeViewItem.Items.Add(radTreeViewItemChild);

            radTreeViewItemChild = new RadTreeViewItem();
            radTreeViewItemChild.Header = "Child3";
            radTreeViewItem.Items.Add(radTreeViewItemChild);

            _radTreeViewItems.Add(radTreeViewItem);
            radTreeView.ItemsSource = _radTreeViewItems;            

            radTreeView.SelectedItem = radTreeView.GetItemByPath(@"TopLevel\\Child2");//if pane is pinned, this works. If pane is unpinned, this returns a null and item is not selected
            if(radTreeView.SelectedItem == null)
            {
                MessageBox.Show("Selected item is null.");
            }
            else
            {
                RadTreeViewItem rtv = radTreeView.SelectedItem as RadTreeViewItem;
                MessageBox.Show("Selected item is " + rtv.Header.ToString());
            }
        }

 

<telerikDocking:RadDocking x:Name="radDockingContainer" >
            <telerikDocking:RadDocking.DocumentHost>
                <telerikDocking:RadSplitContainer  x:Name="gridSplitterContainer" InitialPosition="DockedRight" >
                    <telerikDocking:RadPaneGroup>
                        <telerikDocking:RadPane  CanFloat="False" CanUserClose="False" CanUserPin="True" x:Name="gridPane" >                            
                                <Button Height="22" Width="60" Content="Load" Margin="-100,0,0,0" Click="LoadButton_Click"/>                           
                        </telerikDocking:RadPane>
                    </telerikDocking:RadPaneGroup>
                </telerikDocking:RadSplitContainer>
            </telerikDocking:RadDocking.DocumentHost>
            <telerikDocking:RadSplitContainer InitialPosition="DockedLeft">
                <telerikDocking:RadPaneGroup x:Name="interviewOutputPane">
                    <telerikDocking:RadPane ContextMenuTemplate="{x:Null}" CanFloat="False" CanUserClose="False" AutoHideWidth="280" MinWidth="50" CanUserPin="True" x:Name="interviewTab" Header="Test">
                        <telerik:RadTreeView  x:Name="radTreeView" TextSearch.TextPath="Header" IsVirtualizing="False" >                        
                        </telerik:RadTreeView>
                    </telerikDocking:RadPane>                    
                </telerikDocking:RadPaneGroup>
            </telerikDocking:RadSplitContainer>              
        </telerikDocking:RadDocking>

Thanks,

Scott

0
Petar Mladenov
Telerik team
answered on 10 Aug 2017, 12:11 PM
Hello Scott,

We checked the internal logic behind GetIntemByPath function. It uses the WPF Frameworks' BringItemIntoView and ContainerFromIndex methods which fail when the Visibility of the control is collapsed. That is why we encourage you to switch your approach to finding and selecting the model somehow. Once visible, the Style which binds the IsSelected of the RadTreeViewItem will kick in.

Something concerning we would like to note. You use the ItemsSource of the RadTreeView in which you insert RadTreeViewItems. By using ItemsSource, you turn on the databinding mechanism of RadTreeView which is ItemsControl and it will create containers for the objects in the source collection. The containers of RadTreeView are RadTreeViewItems. So in your case you will produce additional, unexpected nesting of RadTreeViewItems which might lead to various failures of features. What you should do is either use the Items collection of the RadTreeView and RadTreeViewItem (which are both ItemControls) and insert RadTreeViewItems, or use the ItemsSource populated with non-visual classes - that do not inherit from Control, FrameWorkElement, UIElement, etc.

Regards,
Petar Mladenov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Scott Michetti
Top achievements
Rank 1
answered on 10 Aug 2017, 01:56 PM

Thanks for the reply Petar. I think I will access the model as you recommend to deal with my issue. I will also look at your recommendation regarding the way I am binding RadTreeViewItems.

Scott

Tags
TreeView
Asked by
Scott Michetti
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Scott Michetti
Top achievements
Rank 1
Share this question
or