0

我们在我们的应用程序中使用 Telerik RadTreeview。我们在代码中使用了 MVVM 模式。

长话短说,我需要在树视图中选择一个特定项目,展开它,然后将该项目滚动到我的视图中。我在我的视图模型中设置了 SelectedReportRoot。

我按照下面链接中显示的示例创建了我自己的扩展 radtreeview。

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

我的问题是性能。这不好。我做了调试,发现找到一个项目需要很多时间,事实上我的 IE 挂了。谁能告诉我如何解决这个问题,或者向我指出一些有用的资源。

 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>
4

1 回答 1

1

我能够解决我的问题。请看下面我做了什么。总是欢迎评论和建议来改进我的代码。

在我的 UI 中,我在 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>

我在下面添加

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

我的 ItemsSource 是 ReportRootEntityViewModel 的集合。在此,我添加了三个新属性。

一个是 ReportRootEntityViewModel 类型的父级。

两个布尔属性 IsSelected 和 IsExpanded

现在只需获取正确的元素并设置如下属性即可

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;
            }  
于 2013-04-01T23:43:08.553 回答