TreeView 中的 ContainerFromItem returns 空
ContainerFromItem at TreeView returns null
我用一个根元素创建了一个 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 不为空?
P.S。我读 Treeview ContainerFromItem always returns null and also TreeView.ItemContainerGenerator.ContainerFromItem returns 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>
在 TreeNode 处:
private bool _isExpanded;
public bool IsExpanded
{
get
{
return _isExpanded;
}
set
{
_isExpanded = value;
OnPropertyChanged("IsExpanded");
}
}
因此,select 根元素,获取其子元素并尝试找到此项:
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>
它对我有用
我用一个根元素创建了一个 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 不为空?
P.S。我读 Treeview ContainerFromItem always returns null and also TreeView.ItemContainerGenerator.ContainerFromItem returns 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>
在 TreeNode 处:
private bool _isExpanded;
public bool IsExpanded
{
get
{
return _isExpanded;
}
set
{
_isExpanded = value;
OnPropertyChanged("IsExpanded");
}
}
因此,select 根元素,获取其子元素并尝试找到此项:
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>
它对我有用