diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SilverlightGlimpse/Controls/BrokenBindingsViewer.xaml.cs	Sat Apr 21 19:20:28 2012 +0100
@@ -0,0 +1,92 @@
+using System.Reflection;
+using System.Windows.Data;
+using SilverlightGlimpse.Models;
+using System.Windows;
+using System.Diagnostics;
+using System.Windows.Media;
+using SilverlightGlimpse.Services;
+
+namespace SilverlightGlimpse.Controls
+{
+    public partial class BrokenBindingsViewer
+    {
+        public BrokenBindingsViewer()
+        {
+            InitializeComponent();
+        }
+
+        private void BrokenBindings_Loaded(object sender, RoutedEventArgs e)
+        {
+            this.icBrokenBindings.Items.Clear();
+            LoadBrokenBindings(GlimpseService.CreateInstance.RootVisual);
+        }
+
+        private void LoadBrokenBindings(UIElement uiElement)
+        {
+            var frameworkElement = uiElement as FrameworkElement;
+
+            if (frameworkElement != null)
+            {
+                foreach (var fieldInfo in frameworkElement.GetType().GetFields(BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Static))
+                {
+                    if (object.ReferenceEquals(fieldInfo.FieldType, typeof(DependencyProperty)))
+                    {
+                        var bindingExpression = frameworkElement.GetBindingExpression((DependencyProperty)fieldInfo.GetValue(null));
+
+                        if (bindingExpression != null && bindingExpression.ParentBinding.Source == null && bindingExpression.ParentBinding.RelativeSource == null)
+                        {
+                            var isInherited = false;
+
+                            if (frameworkElement.DataContext != null && !string.IsNullOrEmpty(bindingExpression.ParentBinding.Path.Path))
+                            {
+                                foreach (var propertyInfo in frameworkElement.DataContext.GetType().GetProperties(BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Instance))
+                                {
+                                    if (string.Compare(propertyInfo.Name, bindingExpression.ParentBinding.Path.Path) == 0)
+                                    {
+                                        isInherited = true;
+                                        break; // TODO: might not be correct. Was : Exit For
+                                    }
+                                }
+                            }
+
+                            if (isInherited)
+                            {
+                                break; // TODO: might not be correct. Was : Exit For
+                            }
+
+                            //this code handles empty bindings on the Button controls
+                            //I'll have to look into why the Button has an empty or unresolved binding
+                            if (string.IsNullOrEmpty(frameworkElement.Name) 
+                                && frameworkElement.GetType().Name == "TextBlock" 
+                                && fieldInfo.Name == "TextProperty" 
+                                && string.IsNullOrEmpty(bindingExpression.ParentBinding.Path.Path))
+                            {
+                                break; // TODO: might not be correct. Was : Exit For
+                            }
+
+                            BrokenBinding objBrokenBinding = new BrokenBinding(
+                                frameworkElement.Name, 
+                                frameworkElement.GetType().Name, 
+                                fieldInfo.Name, 
+                                bindingExpression.ParentBinding.Path.Path);
+                            this.icBrokenBindings.Items.Add(objBrokenBinding);
+                            Debug.WriteLine("Broken Binding - ", objBrokenBinding.ToString());
+                        }
+                    }
+                }
+
+                int children = VisualTreeHelper.GetChildrenCount(frameworkElement);
+
+                for (int j = 0; j <= children - 1; j++)
+                {
+                    FrameworkElement child = VisualTreeHelper.GetChild(frameworkElement, j) as FrameworkElement;
+
+                    if (child != null)
+                    {
+                        LoadBrokenBindings(child);
+                    }
+                }
+            }
+        }
+    }
+}
\ No newline at end of file