view SilverlightGlimpse/Controls/BrokenBindingsViewer.xaml.cs @ 60:fc62c971a117

upload to blog
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sat, 21 Apr 2012 21:19:39 +0100
parents 3591c26bd63e
children
line wrap: on
line source

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);
                    }
                }
            }
        }
    }
}