Solved

WPF Bind to an item generated by Datatemplate using ElementName

Posted on 2016-09-28
3
40 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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