1

TreeView用一个根元素创建了一个:

TreeViewControl treeViewConrol = new TreeViewControl(tvVisual,TreeServiceManager);
TreeNode rootNode=GetRootNode();

然后,我将此根元素添加到树中:

treeViewControl.TreeView.Items.Add(rootNode);

并尝试定位项目:

TreeViewControl.LocateItem(ref treeViewConrol.TreeView, rootNode);

public static  void LocateItem(ref TreeView treeView, object item)
{
    ExpandAndSelectItem(treeView, item);
}

private static bool ExpandAndSelectItem(ItemsControl parentContainer, object itemToSelect)
{
    //check all items at the current level
    foreach (TreeNode item in parentContainer.Items)
    {
        TreeViewItem currentContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

        //if the data item matches the item we want to select, set the corresponding
        //TreeViewItem IsSelected to true
        if (item == itemToSelect && currentContainer != null)
        {
            currentContainer.IsSelected = true;
            currentContainer.BringIntoView();
            currentContainer.Focus();

            //the item was found
            return true;
        }
    }

    //cut part shippet's code. You can find it at blog

    //no item was found
    return false;
}

因此,我从http://blog.quantumbitdesigns.com获取了这段代码片段。但是这条线:

TreeViewItem currentContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

始终为空。

我的观点是从根元素获取容器,获取它的子元素并通过,同时我可以找到元素并展开树。

那么,如何让 currentContainer 不为空呢?

PS我读过Treeview ContainerFromItem 总是返回 null并且TreeView.ItemContainerGenerator.ContainerFromItem 返回 null

4

2 回答 2

2

答案是 :

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}" >
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />    
        <Setter Property="FontWeight" Value="Normal" />    
    </Style>
</TreeView.ItemContainerStyle>

在 TreeNode :

private bool _isExpanded;       
public bool IsExpanded
{
    get
    {
        return _isExpanded;
    }
    set
    {
        _isExpanded = value;
        OnPropertyChanged("IsExpanded");
    }
}

所以,选择根元素,获取它的子元素并尝试找到这个项目:

public void  LocateItem(TreeNode item ,TreeNode rootNode)
{
    Queue<TreeNode> queue = new Queue<TreeNode>();
    queue.Enqueue(rootNode); //push at queue root elem

    bool found = false;
    while(queue.Any()) // while queue has any item (or when we find item)
    {
        var p = queue.Peek(); // get first item from queue
        var children = p.Children; // get the children from item

        if (children.Any())
        {
            for (int i = 0; i < children.Count; i++) 
            {
                if (children[i].Equals(item))
                {
                    item.IsExpanded = true;
                    found = true;
                    var parent = item.Parent;
                    int whileCount = 0; //level limit
                    while(whileCount<10000)//this tree very big
                    {                               
                        if (parent != null) //expand all parents too.
                        {
                            parent.IsExpanded = true;
                        }
                        else
                        }
                            break;
                        }

                        parent = parent.Parent;
                        whileCount = whileCount + 1;
                    }

                    break; //we found the element, break.
                }
            }
        }

        queue.Dequeue(); //remove item from queue

        if (!found)//if not found
        {
            foreach (var ch in children)
                queue.Enqueue(ch); //push children into queue
        }
    }
}
于 2015-07-17T07:31:07.400 回答
2

试试这个。

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}" >
        <Setter Property="IsExpanded" Value="True" />        
    </Style>
</TreeView.ItemContainerStyle>

它对我有用

于 2018-04-25T12:29:36.193 回答