我们在我们的应用程序中使用telerik RadTreeview。我们在代码中使用MVVM模式。
长话短说,我需要在树视图中选择一个特定项目,展开它,然后将该项目滚动到我的视图中。我在viewmodel中设置了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>
答案 0 :(得分: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}">
My 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;
}