Reputation: 323
I am using a TreeListView (ObjectListView) and populated it with a number of items from 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>();
}
}
How can I select an object (node), scroll tree and expand parent nodes to it (if necessary)? I tried this:
var node = data.SelectMany(x => GetChildren(x)).Where(x => x.ID == 100).FirstOrDefault();
if (node != null)
{
this.tlv.Select();
this.tlv.SelectObject(node, true);
<???>
}
For the average WinForms TreeView my code is as follows:
treeView1.SelectedNode = findNodes[j];
findNodes[j].EnsureVisible();
WinAPI.SendMessage(treeView1.Handle, WinAPI.WM_HSCROLL, (IntPtr)WinAPI.SB_LEFT, IntPtr.Zero);
Upvotes: 0
Views: 1274
Reputation: 6882
ObjectListView
has a Reveal()
method that does exactly this. From the docs:
Unroll all the ancestors of the given model and make sure it is then visible.
So your code should be simply:
this.tlv.Reveal(node, true);
Upvotes: 3