查找动态添加的ScatterViewItem

时间:2012-11-07 22:25:51

标签: c# wpf pixelsense

我有以下内容:

Window window = Application.Current.Windows.Cast<Window>().SingleOrDefault(x => x.IsActive);
ScatterView main = UIHelper.FindChild<ScatterView>(window, "MainScatterView");
        main.Items.Add(type);

//Neither of these work
ScatterViewItem parent = (ScatterViewItem)main.ContainerFromElement(type);

ScatterViewItem parent = UIHelper.FindVisualParent<ScatterViewItem>(type);

文档说:当您添加一个非ScatterViewItem类型的对象时,ScatterView控件首先将该对象包装在ScatterViewItem控件中,然后再将其添加到集合中 http://msdn.microsoft.com/en-us/library/microsoft.surface.presentation.controls.scatterviewitem.aspx

如何获得已包装的scatterviewitem?

1 个答案:

答案 0 :(得分:2)

与其他ItemsControl一样,ScatterView通过ItemContainerGenerator创建和管理其项容器,它提供了获取某个项目的容器或获取某个容器的项目的方法:

ScatterView scatterView = ...
object item = ...
ScatterViewItem scatterViewItem =
    scatterView.ItemContainerGenerator.ContainerFromItem(item) as ScatterViewItem;

ScatterViewItem scatterViewItem = ...
object item = scatterView.ItemContainerGenerator.ItemFromContainer(scatterViewItem);