Search code examples
c#wpfpixelsense

Finding a dynamically added ScatterViewItem


I have the following:

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);

The documentation says: When you add an object that is not of type ScatterViewItem, the ScatterView control first wraps the object in a ScatterViewItem control before adding it to the collection http://msdn.microsoft.com/en-us/library/microsoft.surface.presentation.controls.scatterviewitem.aspx

How does one get the scatterviewitem that it has been wrapped in?


Solution

  • As every other ItemsControl, ScatterView creates and manages its item containers by an ItemContainerGenerator, which provides methods to get the container for a certain item or to get the item for a certain container:

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

    or

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