Public Class CustomEditTemplateSelector
Inherits DataTemplateSelector
Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) _
As DataTemplate
Dim dataTemplate As DataTemplate = Nothing
Dim editor = TemplateEditor.GetEditor(container)
' If the Tag property is set this means that an edit template was already
' returned and we need to return the same template to prevent the editor
' to exit edit mode.
If TypeOf editor.Tag Is DataTemplate Then
Return TryCast(editor.Tag, DataTemplate)
End If
If item IsNot Nothing Then
If CInt(item) > 10000 Then
dataTemplate = TryCast(editor.FindResource("editTemplateRed"), DataTemplate)
Else
dataTemplate = TryCast(editor.FindResource("editTemplateGreen"), DataTemplate)
End If
End If
If dataTemplate IsNot Nothing Then
editor.Tag = dataTemplate
Return dataTemplate
End If
Return MyBase.SelectTemplate(item, container)
End Function
End Class