Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
VisualExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Text;
7 using System.Windows.Documents;
8 using System.Windows.Media;
9 
10 namespace SiliconStudio.Presentation.Extensions
11 {
12  public static class VisualExtensions
13  {
14  public static Visual FindAdornable(this Visual source)
15  {
16  return FindAdornableOfType<Visual>(source);
17  }
18 
19  public static T FindAdornableOfType<T>(this Visual source) where T : Visual
20  {
21  if (source == null)
22  throw new ArgumentNullException("source");
23 
24  if (AdornerLayer.GetAdornerLayer(source) != null && source is T)
25  return (T)source;
26 
27  int childCount = VisualTreeHelper.GetChildrenCount(source);
28  for (int i = 0; i < childCount; i++)
29  {
30  var child = VisualTreeHelper.GetChild(source, i) as T;
31  if (child != null)
32  {
33  var test = child.FindAdornableOfType<T>();
34  if (test != null)
35  return test;
36  }
37  }
38 
39  return null;
40  }
41 
42  public static AdornerLayer FindAdornerLayer(this Visual source)
43  {
44  if (source == null)
45  throw new ArgumentNullException("source");
46 
47  var adornerLayer = AdornerLayer.GetAdornerLayer(source);
48  if (adornerLayer != null)
49  return adornerLayer;
50 
51  int childCount = VisualTreeHelper.GetChildrenCount(source);
52  for (int i = 0; i < childCount; i++)
53  {
54  var child = VisualTreeHelper.GetChild(source, i) as Visual;
55  if (child != null)
56  {
57  var test = child.FindAdornerLayer();
58  if (test != null)
59  return test;
60  }
61  }
62 
63  return null;
64  }
65 
66  }
67 }
static AdornerLayer FindAdornerLayer(this Visual source)