Solved

WPF Bind to an item generated by Datatemplate using ElementName

Posted on 2016-09-28
3
49 Views
Last Modified: 2016-09-30
Hi Guys,

I have a problem trying to bind to an element created within a datatemplate that is part of a content presenter.

I have attached my XAML with the highlighted sections being the problematic areas. I need to bind my my bespoke behaviour called
local:MyColumnsBindingBehavior.ListBox found in the main content grid to the listbox element name called "listBoxColumns" found in the "columnTemplate" DataTemplate which is part of the "dialogcontent" ContentPresenter.

The basic premise is how to bind to an control generated inside a datatemplate. I am aware of using the VisualTreeHelper like below but don't know how to use it to bind to the control in question:

    Private Function FindVisualChild(Of childItem As DependencyObject)(ByVal obj As DependencyObject) As childItem
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(obj) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(obj, i)
            If child IsNot Nothing AndAlso TypeOf child Is childItem Then
                Return CType(child, childItem)
            Else
                Dim childOfChild As childItem = FindVisualChild(Of childItem)(child)
                If childOfChild IsNot Nothing Then
                    Return childOfChild
                End If
            End If
        Next i
        Return Nothing
    End Function

Open in new window


OR

    Private Shared Function GetFrameworkElementByName(Of T As FrameworkElement)(referenceElement As FrameworkElement) As T
        Dim child As FrameworkElement = Nothing
        For i As Int32 = 0 To VisualTreeHelper.GetChildrenCount(referenceElement) - 1
            child = TryCast(VisualTreeHelper.GetChild(referenceElement, i), FrameworkElement)
            System.Diagnostics.Debug.WriteLine(child)
            If child IsNot Nothing AndAlso child.[GetType]() = GetType(T) Then
                Exit For
            ElseIf child IsNot Nothing Then
                child = GetFrameworkElementByName(Of T)(child)
                If child IsNot Nothing AndAlso child.[GetType]() = GetType(T) Then
                    Exit For
                End If
            End If
        Next
        Return TryCast(child, T)
    End Function

End Class

Open in new window


But not sure how this is to be used perhaps in a converter?

I apologise for the rather confusing question but I hope you understand what I am trying to do and would appreciate any help as  soon as possible.

If a converter is to be used C# or VB solutions are welcom
MyXAML.docx
0
Comment
Question by:databarracks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 

Author Comment

by:databarracks
ID: 41823018
Can anybody help me?
0
 

Accepted Solution

by:
databarracks earned 0 total points
ID: 41823649
This was pointless as no help as usual if it isn't a database quesiton.
0
 

Author Closing Comment

by:databarracks
ID: 41823651
I had no help on this. Will go to StackOverflow from now if it isn't database related
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question