【发布时间】: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