59
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
1 using System.Reflection;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
2 using System.Windows.Data;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
3 using SilverlightGlimpse.Models;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
4 using System.Windows;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
5 using System.Diagnostics;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
6 using System.Windows.Media;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
7 using SilverlightGlimpse.Services;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
8
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
9 namespace SilverlightGlimpse.Controls
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
10 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
11 public partial class BrokenBindingsViewer
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
12 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
13 public BrokenBindingsViewer()
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
14 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
15 InitializeComponent();
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
16 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
17
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
18 private void BrokenBindings_Loaded(object sender, RoutedEventArgs e)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
19 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
20 this.icBrokenBindings.Items.Clear();
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
21 LoadBrokenBindings(GlimpseService.CreateInstance.RootVisual);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
22 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
23
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
24 private void LoadBrokenBindings(UIElement uiElement)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
25 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
26 var frameworkElement = uiElement as FrameworkElement;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
27
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
28 if (frameworkElement != null)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
29 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
30 foreach (var fieldInfo in frameworkElement.GetType().GetFields(BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Static))
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
31 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
32 if (object.ReferenceEquals(fieldInfo.FieldType, typeof(DependencyProperty)))
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
33 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
34 var bindingExpression = frameworkElement.GetBindingExpression((DependencyProperty)fieldInfo.GetValue(null));
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
35
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
36 if (bindingExpression != null && bindingExpression.ParentBinding.Source == null && bindingExpression.ParentBinding.RelativeSource == null)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
37 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
38 var isInherited = false;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
39
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
40 if (frameworkElement.DataContext != null && !string.IsNullOrEmpty(bindingExpression.ParentBinding.Path.Path))
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
41 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
42 foreach (var propertyInfo in frameworkElement.DataContext.GetType().GetProperties(BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Instance))
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
43 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
44 if (string.Compare(propertyInfo.Name, bindingExpression.ParentBinding.Path.Path) == 0)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
45 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
46 isInherited = true;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
47 break; // TODO: might not be correct. Was : Exit For
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
48 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
49 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
50 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
51
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
52 if (isInherited)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
53 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
54 break; // TODO: might not be correct. Was : Exit For
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
55 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
56
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
57 //this code handles empty bindings on the Button controls
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
58 //I'll have to look into why the Button has an empty or unresolved binding
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
59 if (string.IsNullOrEmpty(frameworkElement.Name)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
60 && frameworkElement.GetType().Name == "TextBlock"
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
61 && fieldInfo.Name == "TextProperty"
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
62 && string.IsNullOrEmpty(bindingExpression.ParentBinding.Path.Path))
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
63 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
64 break; // TODO: might not be correct. Was : Exit For
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
65 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
66
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
67 BrokenBinding objBrokenBinding = new BrokenBinding(
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
68 frameworkElement.Name,
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
69 frameworkElement.GetType().Name,
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
70 fieldInfo.Name,
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
71 bindingExpression.ParentBinding.Path.Path);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
72 this.icBrokenBindings.Items.Add(objBrokenBinding);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
73 Debug.WriteLine("Broken Binding - ", objBrokenBinding.ToString());
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
74 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
75 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
76 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
77
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
78 int children = VisualTreeHelper.GetChildrenCount(frameworkElement);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
79
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
80 for (int j = 0; j <= children - 1; j++)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
81 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
82 FrameworkElement child = VisualTreeHelper.GetChild(frameworkElement, j) as FrameworkElement;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
83
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
84 if (child != null)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
85 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
86 LoadBrokenBindings(child);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
87 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
88 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
89 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
90 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
91 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
92 } |