I've got a listbox where I have to have virtualization on. I then have a map that has items plotted on it that need to have the feature of letting the user click on them. When they click on an item, I need to bring that item into view in the listbox.
The issue is virtualization. When I try to grab the container, it returns null, which makes sense, however what's the right thing to do? I've tried a few things such as UpdateLayout()
, but I've yet to find the answer. Any ideas?
var container = lstItems.ItemContainerGenerator.ContainerFromItem(clickedItem);
if (container != null)
{
var exp = container.Descendants().OfType<Expander>().FirstOrDefault();
if (exp != null)
{
exp.IsExpanded = true;
exp.BringIntoView();
}
}
答案 0 :(得分:2)
ListBox
为您提供了完成此操作的方法:
lstItems.ScrollIntoView(clickedItem);
如果您在已经展示的代码上方插入该行,那么container
应该会为您提供实际的ListBoxItem
。
但是,如果该项目不在视图范围内,则可能尚未应用该模板。您可以在搜索container.UpdateLayout()
之前致电Expander
来解决此问题。
但是,你仍然需要exp.BringIntoView()
调用,因为扩展器在扩展后可能会变得更大,并且对ScrollIntoView
的初始调用可能没有将它带到视口中。或者,您可以再次拨打ScrollIntoView
。