Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

WPF Bind to an item generated by Datatemplate using ElementName

Posted on 2016-09-28
3
Medium Priority
?
60 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
  • 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

886 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