PageRenderTime 37ms CodeModel.GetById 28ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/V2/trunk/RI/Desktop/StockTraderRI.Infrastructure/TreeHelper.cs

#
C# | 61 lines | 30 code | 3 blank | 28 comment | 5 complexity | 55c7bf51a849365a7d76b3609952cab2 MD5 | raw file
 1//===================================================================================
 2// Microsoft patterns & practices
 3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
 4//===================================================================================
 5// Copyright (c) Microsoft Corporation.  All rights reserved.
 6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 9// FITNESS FOR A PARTICULAR PURPOSE.
10//===================================================================================
11// The example companies, organizations, products, domain names,
12// e-mail addresses, logos, people, places, and events depicted
13// herein are fictitious.  No association with any real company,
14// organization, product, domain name, email address, logo, person,
15// places, or events is intended or should be inferred.
16//===================================================================================
17using System;
18using System.Windows;
19
20namespace StockTraderRI.Infrastructure
21{
22    /// <summary>
23    /// Helper class used to traverse the Visual Tree.
24    /// </summary>
25    public static class TreeHelper
26    {
27        /// <summary>
28        /// Traverses the Visual Tree upwards looking for the ancestor that satisfies the <paramref name="predicate"/>.
29        /// </summary>
30        /// <param name="dependencyObject">The element for which the ancestor is being looked for.</param>
31        /// <param name="predicate">The predicate that evaluates if an element is the ancestor that is being looked for.</param>
32        /// <returns>
33        /// The ancestor element that matches the <paramref name="predicate"/> or <see langword="null"/>
34        /// if the ancestor was not found.
35        /// </returns>
36        public static DependencyObject FindAncestor(DependencyObject dependencyObject, Func<DependencyObject, bool> predicate)
37        {
38            if (predicate(dependencyObject))
39            {
40                return dependencyObject;
41            }
42
43            DependencyObject parent = null;
44#if SILVERLIGHT
45            FrameworkElement frameworkElement = dependencyObject as FrameworkElement;
46            if (frameworkElement != null)
47            {
48                parent = frameworkElement.Parent ?? System.Windows.Media.VisualTreeHelper.GetParent(frameworkElement);
49            }
50#else
51            parent = LogicalTreeHelper.GetParent(dependencyObject);
52#endif
53            if (parent != null)
54            {
55                return FindAncestor(parent, predicate);
56            }
57
58            return null;
59        }
60    }
61}