【问题标题】:Select (highlight) node in TreeListView在 TreeListView 中选择(突出显示)节点
【发布时间】:2015-02-03 09:35:13
【问题描述】:

我正在使用 TreeListView (ObjectListView) 并用 DB 中的一些项目填充它:

class Data
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<Data> Child;

    public Data(int id, string name)
    {
        ID = id;
        Name = name;
        Child = new List<Data>();
    }
}

如何选择对象(节点)、滚动树并将父节点展开到它(如果需要)?我试过这个:

var node = data.SelectMany(x => GetChildren(x)).Where(x => x.ID == 100).FirstOrDefault();
if (node != null)
{                   
    this.tlv.Select();
    this.tlv.SelectObject(node, true);
    <???>
}

对于普通的 WinForms TreeView,我的代码如下:

treeView1.SelectedNode = findNodes[j];
findNodes[j].EnsureVisible();
WinAPI.SendMessage(treeView1.Handle, WinAPI.WM_HSCROLL, (IntPtr)WinAPI.SB_LEFT, IntPtr.Zero);

【问题讨论】:

  • tlv.SelectObject 就足够了。 TLV 通过引用比较对象。从data 中选择的对象node 是否存在于TLV 的对象列表中(具有相同的引用)?也许您从不同的数据库查询中填充了 TLV 数据。
  • treeView1.ExpandAll() 时有效。在这种情况下,我会使用SelectObject(node)EnsureModelVisible(node)。但是当树被折叠时,这些方法不起作用并且treeListView1.GetParent(node) return null (我如何获得所有的父母,然后展开它们?)

标签: c# winforms objectlistview


【解决方案1】:

ObjectListView 有一个 Reveal() 方法可以做到这一点。来自文档:

展开给定模型的所有祖先并确保其可见。

所以你的代码应该很简单:

this.tlv.Reveal(node, true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-23
    • 1970-01-01
    • 2010-10-26
    • 2012-12-15
    相关资源
    最近更新 更多