ContainerFromItem at TreeView returns null
本文关键字:null returns TreeView at ContainerFromItem | 更新日期: 2023-09-27 18:02:54
我创建了一个具有一个根元素的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不空?
注:我读到Treeview ContainerFromItem总是返回null和Treeview . itemcontainergenerator .ContainerFromItem返回null
答案是:
<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");
}
}
那么,选择根元素,找到它的子元素并尝试找到这个项目:
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
}
}
}
试试这个
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" >
<Setter Property="IsExpanded" Value="True" />
</Style>
</TreeView.ItemContainerStyle>
It's Working For Me