Disable bring item into view when node is selected programatically

4 posts, 1 answers
  1. Jiti
    Jiti avatar
    12 posts
    Member since:
    Jul 2012

    Posted 27 Feb 2013 Link to this post

    Hi,

    I want to implement a lock feature on the treeview. If the tree is locked I do not want the tree to bring into view the selected item. It seems to be the default behavior of the tree to expand to the selected node if only one of the nodes in the path to the node is collapsed. It does not happen if the node is not already loaded or if there are 2 or more nodes in the path that are collapsed.
    For e.g.
    I load the tree and navigate to the path 0|1|1|2
    I then collapse to 0|1 and lock the tree.
    When I select 0|1|1|2 programatically, the tree automatically expands to show 0|1|1|2 selected and brings it into view.

    Is there a way to disable this behavior? I want the tree to stay at 0|1 level. I also want the node 0|1|1|2 to be selected. (So that when I interactively expand via GUI to 0|1|1|2 it will show up selected.)

    Thanks,
    Jiti
  2. Jiti
    Jiti avatar
    12 posts
    Member since:
    Jul 2012

    Posted 28 Feb 2013 Link to this post

    I think I can get around the tree getting expanded when it is in a "locked" state by handling the PreviewExpanded event for the radtreeview. But I need a way to know if the event was triggered by the user or by the API. 
    I tried looking at the UserInitiated property (although it is private), but it was false both when the expansion was happening because of a user click or by the tree API. 

    Is there a way to tell the difference?

  3. UI for WPF is Visual Studio 2017 Ready
  4. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 04 Mar 2013 Link to this post

    Hello Jiti,

    I posted a reply in the support thread you started but I will post the same information here as well, in case anyone else from the community encounters the same issue.

    The RadTreeView tries to bring every selected item into view. However, it won't be able to bring into view an item that hasn't been loaded. And it is important to note that the RadTreeView control loads/initializes its items only when they are displayed (expanded) - which explains why a selected item is brought into view only if its path was previously expanded.

    However, there isn't a built-in feature or a method that allows you to change that behavior easily. But we can offer workarounds. For example, you can virtualize your RadTreeView control to disable the BringIntoView feature. When the RadTreeView control is virtualized, it only prepares containers for the visible and expanded items. This is why when you collapse the path to an item, even if you select the item, the BringIntoView() method won't be able to find generated containers for the parent items of the selected node and the items will remain collapsed. If you decide to use this as a workaround you can simply bind the TreeViewPanel.IsVirtualizing property to a property from your MainViewModel and control the value of the property through a custom logic(or a CheckBox):
    Copy Code
    <telerik:RadTreeView Grid.Row="1" x:Name="xTreeView"
    telerik:TreeViewPanel.IsVirtualizing="{Binding IsLocked}"
    ItemsSource="{Binding Items}" ItemTemplate="{StaticResource TreeItemTemplate}" />

    Another approach that you can use is to create a static property in your MainViewModel (let's say it's named IsExpansionSuppressed) and use its value to deny a change in the expanded state of the RadTreeViewItems. You can implement this scenario in an MVVM application where the RadTreeViewItem IsExpanded property is data bound to a business property. And you can use the MainViewModel static property to stop a change in the expanded business property setter. In case you'd like to use this approach, it is important to make sure to set the IsExpansionSuppressed property to True just before selecting a designated item and you need to set the IsExpansionSuppressed back to False, as soon as the selection is changed.
    Basically if you use a button Click event handler to select an item, you can implement the following logic:
    // select 0.1.1.2
    private void RadButton_Click(object sender, RoutedEventArgs e)
    {
        MainViewModel vm = this.DataContext as MainViewModel;
        DataItem item = (this.DataContext as MainViewModel).Items[0].Children[1].Children[1].Children[2];
        if (item != null)
        {
            if (vm.IsLocked)
            {
                MainViewModel.IsExpansionSuppressed = true;
                item.Selected = true;
                MainViewModel.IsExpansionSuppressed = false;
            }
            else
                item.Selected = true;
        }
    }
    And the DataItem class (that describes the RadTreeViewItem data) should contain the following Expanded property setter:
    public bool Expanded
    {
        get
        {
            return this._expanded;
        }
        set
        {
            if (this._expanded != value)
            {
                if (!MainViewModel.IsExpansionSuppressed)
                    this._expanded = value;
                OnPropertyChanged("Expanded");
            }
        }
    }

    I hope this information helps.

    Regards,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  5. Jiti
    Jiti avatar
    12 posts
    Member since:
    Jul 2012

    Posted 04 Mar 2013 Link to this post

    Tina,

    I used the second approach that you described and it is working great for me.
    Thanks!

    Jiti
Back to Top