none
[UWP][VB][XAML]Using DataTemplateSelector.GetElement RRS feed

  • Question

  • I am creating a DataTemplateSelector, and need to get the root of the resulting DataTemplate. I would like to use DataTemplateSelector.GetElement, which I have ever used before. However, I am unsure about how to do two things:

    1. Where do I use this code? If I put it in one of the SelectTemplateCore methods, I would need to put it after the statement that returns the appropriate DataTemplate, but that would mean it would never get called, so that would obviously not work.

    2. I have never used the ElementFactoryGetArgs class before, so I have no idea what to do with that.

    I'm sure this is a very simple thing to do once I see it, but I could not find any examples, so if anybody could either give me a nice simple example, or direct me to somewhere I can find one, it would be greatly appreciated. I have used DataTemplate & DataTemplateSelector many times, so I know how to use them, it is just the GetElement method that I am unfamiliar with. Thanks.


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, August 13, 2019 7:05 PM

All replies

  • Hi Nathan,

    Here are my advices:

    1. Use it in ItemClicked or other events, make sure the DataTemplate has been rendered when calling this method.

    2. Here is used ElementFactoryGetArgs example (use it in ItemClicked event):

    Private Sub TestListView_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs) Dim selector = TestListView.ItemTemplateSelector Dim factory = New ElementFactoryGetArgs() With { .Data = e.ClickedItem, .Parent = TryCast(sender, UIElement) } Dim ele = selector.GetElement(factory)

    // other code End Sub


    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, August 14, 2019 6:34 AM
  • I think you may be misunderstanding my goal. The root of my Template(s) is always a Grid, and in the ContentControl(s) in my XAML that use the DataTemplateSelector I set the Tag to a literal value. What I was hoping to do was assign this value (by using the container parameter of SelectTemplateCore, which would be that ContentControl) to the Tag property of the root Grid (the UIElement returned by DataTemplateSelector.GetElement). I want this to happen when the template is selected (because the Children of the Grid will be binding to the Grid's Tag). Would it be appropriate to put this in the ContentControl's Loaded or Loading event? I was hoping to keep it all in the DataTemplateSelector, but if I need to put it in an Event, I would guess it would be one of these. Is putting it in an Event the only way? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, August 14, 2019 7:52 PM
  • Hi,

    This can be helpful if you can provide your layout code.

    Or you can try to get the node by calling GetElement method in SelectTemplateCore:

    Protected Overrides Function SelectTemplateCore(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
        If TypeOf container Is Grid Then
            Dim element = GetElement(New ElementFactoryGetArgs() With {
                .Data = item,
                .Parent = TryCast(container, UIElement)
            })
        End If
    End Function

    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 15, 2019 1:53 AM
  • If I understand DataTemplateSelector correctly, SelectTemplateCore is supposed to choose & return a DataTemplate (is that correct?). In the code you provide (calling GetElement in SelectTemplateCore), SelectTemplateCore has not yet chosen or returned a DataTemplate. If a DataTemplate has not yet been chosen, how can GetElement determine the element that should be returned? Also (in my case, at least), container is a ContentControl, not a Grid (a Grid is what I am expecting from GetElement). And I am assuming that the compiler would complain about SelectTemplateCore not returning anything. Am I misunderstanding your code?

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, August 15, 2019 2:35 AM
  • Hi,

    This is my problem, it will be called repeatedly, although it does return a root element in a DataTemplate.

    If the Container is a ContentControl and the Grid is the root element in the DataTemplate, then obviously you can get it through GetElement(). Just like my first answer.

    But as you said, you need to attach the Tag property of the ContentControl to the Grid, which can be done by Binding. But before I see your code, I can only guess. So if you can provide your layout and data structure code, this will be very helpful.

    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 15, 2019 8:50 AM