view Glimpse/Glimpse Controls/BrokenBindingsViewer.xaml.vb @ 72:177a9d1eba10

Latest version
author Steven Hollidge <stevenhollidge@hotmail.com>
date Mon, 23 Apr 2012 17:57:21 +0100
parents 3591c26bd63e
children
line wrap: on
line source

Imports System.Reflection
Imports System.Windows.Data
Partial Public Class BrokenBindingsViewer
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub BrokenBindings_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Me.icBrokenBindings.Items.Clear()
        LoadBrokenBindings(GlimpseService.CreateInstance.RootVisual)
    End Sub

    Private Sub LoadBrokenBindings(ByVal uie As UIElement)

        Dim fwe As FrameworkElement = TryCast(uie, FrameworkElement)

        If fwe IsNot Nothing Then

            For Each fi As FieldInfo In fwe.GetType.GetFields(BindingFlags.Public Or BindingFlags.FlattenHierarchy Or BindingFlags.Instance Or BindingFlags.Static)

                If fi.FieldType Is GetType(DependencyProperty) Then

                    Dim be As BindingExpression = fwe.GetBindingExpression(DirectCast(fi.GetValue(Nothing), DependencyProperty))

                    If be IsNot Nothing AndAlso be.ParentBinding.Source Is Nothing AndAlso be.ParentBinding.RelativeSource Is Nothing Then

                        Dim bolIsInherited As Boolean = False

                        If fwe.DataContext IsNot Nothing AndAlso Not String.IsNullOrEmpty(be.ParentBinding.Path.Path) Then

                            For Each p As PropertyInfo In fwe.DataContext.GetType.GetProperties(BindingFlags.Public Or BindingFlags.FlattenHierarchy Or BindingFlags.Static Or BindingFlags.Instance)

                                If String.Compare(p.Name, be.ParentBinding.Path.Path) = 0 Then
                                    bolIsInherited = True
                                    Exit For
                                End If

                            Next

                        End If

                        If bolIsInherited Then
                            Exit For
                        End If

                        '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 fwe.Name = "" AndAlso fwe.GetType.Name = "TextBlock" AndAlso fi.Name = "TextProperty" AndAlso be.ParentBinding.Path.Path = "" Then
                            Exit For
                        End If

                        Dim objBrokenBinding As New BrokenBinding(fwe.Name, fwe.GetType.Name, fi.Name, be.ParentBinding.Path.Path)
                        Me.icBrokenBindings.Items.Add(objBrokenBinding)
                        System.Diagnostics.Debug.WriteLine(String.Format("Broken Binding - ", objBrokenBinding.ToString))
                    End If

                End If

            Next

            Dim children As Integer = VisualTreeHelper.GetChildrenCount(fwe)

            For intX As Integer = 0 To children - 1

                Dim child As FrameworkElement = TryCast(VisualTreeHelper.GetChild(fwe, intX), FrameworkElement)

                If child IsNot Nothing Then
                    LoadBrokenBindings(child)
                End If

            Next

        End If

    End Sub

End Class