c# - 如果元素来自 ItemSsource,如何在 WPF TreeView 中聚焦元素?

标签 c# .net wpf binding

我也可以解释这个问题,问我如何从 TreeView.Items 中的模型对象中获取 TreeViewItem

我关注 this tutorial而不是 TreeView.Items 集合中的 TreeViewItems 我有那个模型类的对象。

但是,我需要根据其他一些事件来关注某些 TreeView 元素。如果 TreeView.Items 包含 TreeViewItems,我很容易找到需要的一个并使用它的 Focus() 方法。但是由于我现在正在使用绑定(bind),所以我不知道该怎么做。

当然,我可以通过添加 TreeViewItems 对象以编程方式填充 TreeView,但是我试图避免它并遵循更多 WPF 方式来处理绑定(bind)。

最佳答案

TreeViewItem tvItem = (TreeViewItem)treeView
                          .ItemContainerGenerator
                          .ContainerFromItem(item);
tvItem.Focus();

关于c# - 如果元素来自 ItemSsource,如何在 WPF TreeView 中聚焦元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5900259/

相关文章:

c# - Wix - 在哪里安装 Windows 功能

c# - 在 IIS 中访问 .svc 文件时出现 HTTP 404

.net - 在 .NET HttpHandler 中检测字节范围请求

wpf - 寻找 DependencyProperty.Register 快捷方式

wpf datagrid 复制并粘贴您所看到的内容

C#返回类型协变和里氏代换原则

c# - 我们为什么要使用用户控件?

c# - 单元测试方法的有效输入

c# - ScaleTransform 中的 DoubleAnimation

c# - 读取行并保留 xml 文件中的空格