本文整理汇总了C#中System.Windows.FrameworkElement.Ancestors方法的典型用法代码示例。如果您正苦于以下问题:C# FrameworkElement.Ancestors方法的具体用法?C# FrameworkElement.Ancestors怎么用?C# FrameworkElement.Ancestors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.FrameworkElement
的用法示例。
在下文中一共展示了FrameworkElement.Ancestors方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindFrame
/// <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.Ancestors().OfType<ModernFrame>().ToArray();
if (name == null || name == "_self")
{
// find first ancestor frame
return frames.FirstOrDefault();
}
if (name == "_parent")
{
// find parent frame
return frames.Skip(1).FirstOrDefault();
}
if (name == "_top")
{
// 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;
}
示例2: FindFrame
public static FantasyFrame FindFrame(String name, FrameworkElement context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
// collect all ancestor frames
FantasyFrame[] frames = context.Ancestors().OfType<FantasyFrame>().ToArray();
if (String.IsNullOrEmpty(name) || name.Equals(FrameSelf))
{
// find the first frame.
return frames.FirstOrDefault();
}
if (name.Equals(FrameParent))
{
// find parent frame
return frames.Skip(1).FirstOrDefault();
}
if (name.Equals(FrameTop))
{
// find top-most frame
return frames.LastOrDefault();
}
// find ancestor frame having a name matching the target
FantasyFrame frame = frames.FirstOrDefault(f => f.Name == name);
if (frame == null)
{
// find frame in context scope
frame = context.FindName(name) as FantasyFrame;
if (frame == null)
{
// find frame in scope of ancestor frame content
FantasyFrame parent = frames.FirstOrDefault();
if (parent != null && parent.Content != null)
{
FrameworkElement content = parent.Content as FrameworkElement;
if (content != null)
{
frame = content.FindName(name) as FantasyFrame;
}
}
}
}
return frame;
}