2

我正在使用 Silverlight 工具包中的 TreeView 组件,并且正在尝试获取所选节点的父节点。TreeView 绑定到一系列对象,因此直接使用 TreeViewItem 似乎是不可能的。

<toolkit:TreeView SelectedItemChanged="DoStuff" DisplayMemberPath="Name" ItemsSource="{Binding MyCollection}">
    <toolkit:TreeView.ItemTemplate>
        <common:HierarchicalDataTemplate ItemsSource="{Binding MySubCollection}">
            <StackPanel>
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </common:HierarchicalDataTemplate>
    </toolkit:TreeView.ItemTemplate>
</toolkit:TreeView>

有没有办法获取在 DoStuff 事件中选择的项目的父项?

4

4 回答 4

3

只要您下载了最新的 Silverlight 工具包,就可以轻松使用包含的 TreeViewExtensions。

  1. 下载Silverlight 工具包并安装。
  2. 添加对 System.Windows.Controls.Toolkit 的引用(来自 Silverlight 工具包)
  3. 使用 GetParentItem() 扩展方法,如下所示:

    private void DoStuff(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (e.NewValue != null)
        {
            var parent = ((TreeView)sender).GetParentItem(e.NewValue);
            //
            // Do stuff with parent, this snippet updates 
            // a TextBlock showing the name of the current parent
            if (parent != null)
            {
                 Status.Text = parent.ToString();
            }
        }
    }
    
于 2009-10-06T00:01:11.283 回答
1

为什么不让 MySubcollection 中的对象保留对其父级的引用?

于 2009-04-15T15:24:14.737 回答
0

假设您的树的名称是“树”,这应该可以解决问题

((tree.SelectedItem as TreeViewItem).Parent as SomeObjectOfYours)
于 2009-05-20T16:41:22.950 回答
0

Justin Angel写了关于“高级” TreeView 使用的文章,其中包括通过绑定对象在树视图中查找任意元素。由于我对 Silverlight 比较陌生,我不确定是否有更好、更优雅的方法,但您可以使用他的一种方法来查找父级。

我想像这样使用调用:

TreeViewItem trvi = ContainerFromItem(tree, tree.SelectedItem);
MySubCollectionItem parentItem = ItemFromContainer(tree, trvi.Parent); // note the "Parent" here

使用以下扩展方法可从某处获得:

public static TreeViewItem ContainerFromItem(this TreeView treeView, object item)
{
    TreeViewItem containerThatMightContainItem = (TreeViewItem)treeView.ItemContainerGenerator.ContainerFromItem(item);
    if (containerThatMightContainItem != null)
        return containerThatMightContainItem;
    else
        return ContainerFromItem(treeView.ItemContainerGenerator, treeView.Items, item);
}

private static TreeViewItem ContainerFromItem(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, object item)
{
    foreach (object curChildItem in itemCollection)
    {
        TreeViewItem parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
        if (parentContainer == null)
            return null;
        TreeViewItem containerThatMightContainItem = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);
        if (containerThatMightContainItem != null)
            return containerThatMightContainItem;
        TreeViewItem recursionResult = ContainerFromItem(parentContainer.ItemContainerGenerator, parentContainer.Items, item);
        if (recursionResult != null)
            return recursionResult;
    }
    return null;
}

public static object ItemFromContainer(this TreeView treeView, TreeViewItem container)
{
    TreeViewItem itemThatMightBelongToContainer = (TreeViewItem)treeView.ItemContainerGenerator.ItemFromContainer(container);
    if (itemThatMightBelongToContainer != null)
        return itemThatMightBelongToContainer;
    else
        return ItemFromContainer(treeView.ItemContainerGenerator, treeView.Items, container);
}

private static object ItemFromContainer(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, TreeViewItem container)
{
    foreach (object curChildItem in itemCollection)
    {
        TreeViewItem parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
        if (parentContainer == null)
            return null;
        TreeViewItem itemThatMightBelongToContainer = (TreeViewItem)parentContainer.ItemContainerGenerator.ItemFromContainer(container);
        if (itemThatMightBelongToContainer != null)
            return itemThatMightBelongToContainer;
        TreeViewItem recursionResult = ItemFromContainer(parentContainer.ItemContainerGenerator, parentContainer.Items, container) as TreeViewItem;
        if (recursionResult != null)
            return recursionResult;
    }
    return null;
}
于 2009-09-02T11:02:10.887 回答