如何以编程方式选择 WPF TreeView 中的项目? [英] How to programmatically select an item in a WPF TreeView?

查看:26
本文介绍了如何以编程方式选择 WPF TreeView 中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式选择 WPF TreeView 中的项目?ItemsControl 模型似乎阻止了它.

How is it possible to programmatically select an item in a WPF TreeView? The ItemsControl model seems to prevent it.

推荐答案

莫名其妙,真的很痛苦,必须使用ContainerFromItem 来获取容器,然后调用select 方法.

It's a real pain for some strange reason, you have to use ContainerFromItem to get the container, then invoke the select method.

//  selectedItemObject is not a TreeViewItem, but an item from the collection that 
//  populated the TreeView.

var tvi = treeView.ItemContainerGenerator.ContainerFromItem(selectedItemObject) 
          as TreeViewItem;

if (tvi != null)
{
    tvi.IsSelected = true;
}

曾经有一篇关于如何做到这一点的博客条目 此处,但链接现在已失效.

There once was a blog entry on how to do it here, but the link is dead now.

这篇关于如何以编程方式选择 WPF TreeView 中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆