Avatar of databarracks
databarracks
 asked on

WPF Bind to an item generated by Datatemplate using ElementName

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
.NET ProgrammingC#

Avatar of undefined
Last Comment
databarracks

8/22/2022 - Mon
databarracks

ASKER
Can anybody help me?
ASKER CERTIFIED SOLUTION
databarracks

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
databarracks

ASKER
I had no help on this. Will go to StackOverflow from now if it isn't database related
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes