/// <summary> /// Finds the frame identified with given name in the specified context. /// </summary> /// <param name="name">The frame name.</param> /// <param name="context">The framework element providing the context for finding a frame.</param> /// <returns>The frame or null if the frame could not be found.</returns> public static ModernFrame FindFrame(string name, FrameworkElement context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } // collect all ancestor frames var frames = context.AncestorsAndSelf().OfType<ModernFrame>().ToArray(); switch (name) { case null: case FrameSelf: // find first ancestor frame return frames.FirstOrDefault(); case FrameParent: // find parent frame return frames.Skip(1).FirstOrDefault(); case FrameTop: // find top-most frame return frames.LastOrDefault(); } // find ancestor frame having a name matching the target var frame = frames.FirstOrDefault(f => f.Name == name); if (frame != null) return frame; // find frame in context scope frame = context.FindName(name) as ModernFrame; if (frame != null) return frame; // find frame in scope of ancestor frame content var parent = frames.FirstOrDefault(); var content = parent?.Content as FrameworkElement; return content?.FindName(name) as ModernFrame; }
/// <summary> /// Finds the frame identified with given name in the specified context. /// </summary> /// <param name="name">The frame name.</param> /// <param name="context">The framework element providing the context for finding a frame.</param> /// <returns>The frame or null if the frame could not be found.</returns> public static ModernFrame FindFrame(string name, FrameworkElement context) { if (context == null) { throw new ArgumentNullException("context"); } // collect all ancestor frames var frames = context.AncestorsAndSelf().OfType<ModernFrame>().ToArray(); if (name == null || name == FrameSelf) { // find first ancestor frame return frames.FirstOrDefault(); } if (name == FrameParent) { // find parent frame return frames.Skip(1).FirstOrDefault(); } if (name == FrameTop) { // find top-most frame return frames.LastOrDefault(); } // find ancestor frame having a name matching the target var frame = frames.FirstOrDefault(f => f.Name == name); if (frame == null) { // find frame in context scope frame = context.FindName(name) as ModernFrame; if (frame == null) { // find frame in scope of ancestor frame content var parent = frames.FirstOrDefault(); if (parent != null && parent.Content != null) { var content = parent.Content as FrameworkElement; if (content != null) { frame = content.FindName(name) as ModernFrame; } } } } return frame; }