in src/Microsoft.Xaml.Behaviors/Core/ExtendedVisualStateManager.cs [787:884]
private static List<FrameworkElement> FindTargetElements(FrameworkElement control, FrameworkElement templateRoot, Storyboard layoutStoryboard, List<OriginalLayoutValueRecord> originalValueRecords, List<FrameworkElement> movingElements)
{
List<FrameworkElement> targets = new List<FrameworkElement>();
// Of course, any elements currently in motion need to be inspected
if (movingElements != null)
{
targets.AddRange(movingElements);
}
// Also, any element with a layout property being animated
foreach (Timeline timeline in layoutStoryboard.Children)
{
FrameworkElement target = (FrameworkElement)GetTimelineTarget(control, templateRoot, timeline);
if (target != null)
{
if (!targets.Contains(target))
{
targets.Add(target);
}
if (ChildAffectingLayoutProperties.Contains(LayoutPropertyFromTimeline(timeline, false)))
{
Panel panel = target as Panel;
if (panel != null)
{
foreach (FrameworkElement child in panel.Children)
{
if (!targets.Contains(child) && !(child is WrapperCanvas))
{
targets.Add(child);
}
}
}
}
}
}
// and any elements that were animated in previous states
foreach (OriginalLayoutValueRecord originalValueRecord in originalValueRecords)
{
if (!targets.Contains(originalValueRecord.Element))
{
targets.Add(originalValueRecord.Element);
}
if (ChildAffectingLayoutProperties.Contains(originalValueRecord.Property))
{
Panel panel = originalValueRecord.Element as Panel;
if (panel != null)
{
foreach (FrameworkElement child in panel.Children)
{
if (!targets.Contains(child) && !(child is WrapperCanvas))
{
targets.Add(child);
}
}
}
}
}
// Now do the expansion. Note that targets.Count increases as we move through the list.
for (int i = 0; i < targets.Count; i++)
{
FrameworkElement target = targets[i];
FrameworkElement parent = VisualTreeHelper.GetParent(target) as FrameworkElement;
if (movingElements != null && movingElements.Contains(target) && parent is WrapperCanvas)
{
// If this element is being moved currently, then we've wrapped it in a Canvas.
// We don't want to track the Canvas wrapper, so parent up once more.
parent = VisualTreeHelper.GetParent(parent) as FrameworkElement;
}
// If we found a parent, then we can add that parent and all siblings
if (parent != null)
{
if (!targets.Contains(parent))
{
targets.Add(parent);
}
for (int j = 0; j < VisualTreeHelper.GetChildrenCount(parent); j++)
{
FrameworkElement sibling = VisualTreeHelper.GetChild(parent, j) as FrameworkElement;
if (sibling != null && !targets.Contains(sibling) && !(sibling is WrapperCanvas))
{
targets.Add(sibling);
}
}
}
}
return targets;
}