Ziggler
Ziggler

Reputation: 3500

Telerik RadTreeview Select an item using code

We are using telerik RadTreeview in our application. We are using MVVM pattern in our code.

Long story short, I need to select a particular item in my treeview, expand it, and scroll that item into my view. I set SelectedReportRoot in my viewmodel.

I followed the example showed in below link and created my own extended radtreeview.

http://sladapter.blogspot.com/2010/11/how-to-bind-to-silverlight-treeview.html

My problem is performance. It is not good. I did debugging and found that finding an item is taking lot of time and infact my IE is hanging up. Can anybody please tell me how to solve this or else point me to some helpful resources.

 public class SelectableRadTreeViewExtended : RadTreeView
  {

    public SelectableRadTreeViewExtended()
      : base()
    {
      this.SelectionChanged += SelectableRadTreeViewExtended_SelectionChanged;
    }

    void SelectableRadTreeViewExtended_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
    {
      this.SelectedItem = e.AddedItems[0];
      if (SelectedItem != null)
      {

        this.CustomSelectedItem = this.SelectedItem;
      }
    }

    public object CustomSelectedItem
    {
      get { return (object)GetValue(CustomSelectedItemProperty); }
      set { SetValue(CustomSelectedItemProperty, value); }
    }
    public static readonly DependencyProperty CustomSelectedItemProperty = DependencyProperty.Register("CustomSelectedItem", typeof(object), typeof(SelectableRadTreeViewExtended), new PropertyMetadata(OnCustomSelectionItemChanged));

    private static void OnCustomSelectionItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      var obj = d as SelectableRadTreeViewExtended;
      if (e.NewValue != null)
      {
        RadTreeViewItem tvi = obj.FindItemNode(e.NewValue);
        if (tvi != null)
        {
          tvi.IsSelected = true;
          obj.GetScrollHost().ScrollIntoView(tvi);
          obj.UpdateLayout();
        }
      }
    }

    public RadTreeViewItem FindItemNode(object item)
    {
      RadTreeViewItem node = null;
      foreach (object data in this.Items)
      {
        node = this.ItemContainerGenerator.ContainerFromItem(data) as RadTreeViewItem;
        if (node != null)
        {
          if (data == item)
            break;
          node = FindItemNodeInChildren(node, item);
          if (node != null)
            break;
        }
      }
      return node;
    }

    protected RadTreeViewItem FindItemNodeInChildren(RadTreeViewItem parent, object item)
    {
      RadTreeViewItem node = null;
      bool isExpanded = parent.IsExpanded;
      if (!isExpanded) //Can't find child container unless the parent node is Expanded once
      {
        parent.IsExpanded = true;
        parent.UpdateLayout();
      }
      foreach (object data in parent.Items)
      {
        node = parent.ItemContainerGenerator.ContainerFromItem(data) as RadTreeViewItem;
        if (data == item && node != null)
          break;
        node = FindItemNodeInChildren(node, item);
        if (node != null)
          break;
      }
      if (node == null && parent.IsExpanded != isExpanded)
        parent.IsExpanded = isExpanded;
      if (node != null)
        parent.IsExpanded = true;
      return node;
    }
  }



<my:SelectableRadTreeViewExtended ItemsSource="{Binding ReportRoots}"
                             SelectionMode="Single"                                 
                             IsEditable="False"
                             Grid.Row="1"
                             HorizontalAlignment="Stretch"
                             VerticalAlignment="Stretch"
                             x:Name="rtvReportRoots"
                             CustomSelectedItem="{Binding SelectedReportRoot, Mode=TwoWay}">
            <my:SelectableRadTreeViewExtended.ItemTemplate>
                <telerik:HierarchicalDataTemplate ItemsSource="{Binding Children}"
                                                  DataType="ViewModels:ReportRootEntityViewModel">
                    <Grid x:Name="NodeContainer"
                          Background="#01000000">
                        <TextBlock Text="{Binding Path=Name}"
                                   Foreground="Gray"
                                   controls:DropZoneHelper.DropZone="NotApplicable" />
                    </Grid>
                </telerik:HierarchicalDataTemplate>

            </my:SelectableRadTreeViewExtended.ItemTemplate>
        </my:SelectableRadTreeViewExtended>

Upvotes: 0

Views: 4946

Answers (1)

Ziggler
Ziggler

Reputation: 3500

I was able to solve my issue. Please see below for what I did. Comments and suggestions are always welcomed to improve my code.

In my UI I added this under UserControl.Resources

<telerik:ContainerBindingCollection x:Key="BindingCollection">
                <telerik:ContainerBinding PropertyName="IsSelected"
                                          Binding="{Binding IsSelected,Mode=TwoWay}" />
                <telerik:ContainerBinding PropertyName="IsExpanded"
                                          Binding="{Binding IsExpanded, Mode=TwoWay}" />
            </telerik:ContainerBindingCollection>

I add below in my

<telerik:RadTreeView.ItemTemplate>
                <telerik:HierarchicalDataTemplate ItemsSource="{Binding Children}"
                                                  DataType="ViewModels:ReportRootEntityViewModel"
                                                  telerik:ContainerBinding.ContainerBindings="{StaticResource BindingCollection}">

My ItemsSource is collection of ReportRootEntityViewModel. In this, I added three new properties.

One is Parent of type ReportRootEntityViewModel.

Two boolean properties IsSelected and IsExpanded

Now it just matter of getting correct element and setting properties as below

ReportRootEntityViewModel result = GetReportRootNode(Convert.ToInt32(data[0]), reportRoot.Children);
            if (result != null)
            {
              if (_previousSelectedReportRoots != null)
              {
                _previousSelectedReportRoots.Parent.Parent.IsExpanded = false;
                _previousSelectedReportRoots.Parent.IsExpanded = false;
                _previousSelectedReportRoots.IsExpanded = false;

                _previousSelectedReportRoots.IsSelected = false;
              }

              result.Parent.Parent.IsExpanded = true;
              result.Parent.IsExpanded = true;
              result.IsExpanded = true;

              result.IsSelected = true;              
              SelectedReportRoot = result;

              SelectedReportRootDisplayName = data[1];
             DisplayLabel = "Display Name :";
              _previousSelectedReportRoots = SelectedReportRoot;
              return;
            }  

Upvotes: 1

Related Questions