comparison SilverlightGlimpse/Controls/BrokenBindingsViewer.xaml.cs @ 59:3591c26bd63e

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