Solved

WPF Bind to an item generated by Datatemplate using ElementName

Posted on 2016-09-28
3
29 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
Comment Utility
Can anybody help me?
0
 

Accepted Solution

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

Author Closing Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now