我们在我们的应用程序中使用 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>