Admiral Land
Admiral Land

Reputation: 2492

ContainerFromItem at TreeView returns null

I've created a TreeView with one root element:

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

Then, i added this root element into tree:

treeViewControl.TreeView.Items.Add(rootNode);

And try to locate item:

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;
}

So, this code snippet I took from http://blog.quantumbitdesigns.com. But the line:

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

is always null.

My point is to get container from root element, get its child elements and go through while I can find elements and expand the tree.

So, how to get currentContainer not null?

P.S. I read Treeview ContainerFromItem always returns null and also TreeView.ItemContainerGenerator.ContainerFromItem returns null.

Upvotes: 1

Views: 1859

Answers (2)

raghu sathvara
raghu sathvara

Reputation: 196

Try This.

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

It's Working For Me

Upvotes: 2

user2545071
user2545071

Reputation: 1410

The answer is :

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

And at TreeNode :

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

So, select root element, get its children and try to find this item:

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

Upvotes: 2

Related Questions