locked
DataTemplateSelector not being called

    Question

  • I have a DataTemplateSelector defined as follows:
    Public Class FigureBindingTemplateSelector : Inherits DataTemplateSelector
    	Public Property TextFigureBindingTemplate() As DataTemplate
    	Public Property PathFigureBindingTemplate() As DataTemplate
    	Protected Overrides Function SelectTemplateCore(item As Object) As DataTemplate
    		Dim figure As Object = item.GetType().GetRuntimeProperty("Figure").GetValue(item)
    		If TypeOf (figure) Is TextFigureBinding Then : Return Me.TextFigureBindingTemplate
    		ElseIf TypeOf (figure) Is PathFigureBinding Then : Return Me.PathFigureBindingTemplate
    		Else : Return MyBase.SelectTemplateCore(item)
    		End If
    	End Function
    End Class
    The DataTemplateSelector is included in my Page's Resources as follows:
    <ctrl:FigureBindingTemplateSelector x:Key="FBTSelector" TextFigureBindingTemplate="{StaticResource TextFigureBindingTemplate}" PathFigureBindingTemplate="{StaticResource PathFigureBindingTemplate}"/>
    And I have an ItemsControl that uses it as follows:
    <ItemsControl x:Name="itmCurrFigures" ItemTemplateSelector="{StaticResource FBTSelector}">
    	<ItemsControl.ItemsPanel><ItemsPanelTemplate><StackPanel/></ItemsPanelTemplate></ItemsControl.ItemsPanel>
    </ItemsControl>
    However, when I bind the ItemsControl by setting the ItemsSource property, the DataTemplateSelector is never called. I have tried putting a Breakpoint in the DataTemplateSelector, but it is never approached. I know that the binding happens, because my app does display the data I am binding (unformatted, of course, since it is not getting a template). Why is my DataTemplateSelector being ignored? Any help would be appreciated.

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

    Wednesday, February 25, 2015 8:24 PM

Answers

  • I finally found the answer. You will notice in my code that I override SelectTemplateCore:
    Protected Overrides Function SelectTemplateCore(item As Object) As DataTemplate
    But the function I was supposed to override was:
    Protected Overrides Function SelectTemplateCore(item As Object, container As DependencyObject) As DataTemplate
    Notice that the second one has a second parameter, so when I added the following to my selector:
    Protected Overrides Function SelectTemplateCore(item As Object, container As DependencyObject) As DataTemplate
    	Return Me.SelectTemplateCore(item)
    End Function
    It worked! I thought you could override either one, but I guess not. At least it was a simple fix.

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

    Saturday, February 28, 2015 12:39 AM

All replies

  • This sample shows how to use DataTemplateSelector mechanism. https://code.msdn.microsoft.com/The-DataTemplateSelector-76d06a77

    Thursday, February 26, 2015 8:53 AM
  • That does not help. First of all, it is using a ContentControl with a converter, not an ItemsControl (or similar) with an ItemTemplateSelector property. It uses a very different technique to achieve the same thing. It does not explain why my ItemTemplateSelector is being ignored. I have used my technique many times before, but it is not working this time. Any other ideas? Thanks.

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

    Thursday, February 26, 2015 4:18 PM
  • I finally found the answer. You will notice in my code that I override SelectTemplateCore:
    Protected Overrides Function SelectTemplateCore(item As Object) As DataTemplate
    But the function I was supposed to override was:
    Protected Overrides Function SelectTemplateCore(item As Object, container As DependencyObject) As DataTemplate
    Notice that the second one has a second parameter, so when I added the following to my selector:
    Protected Overrides Function SelectTemplateCore(item As Object, container As DependencyObject) As DataTemplate
    	Return Me.SelectTemplateCore(item)
    End Function
    It worked! I thought you could override either one, but I guess not. At least it was a simple fix.

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

    Saturday, February 28, 2015 12:39 AM