I'm writing a custom control for WPF.
My custom control has (among others) a dependency property "ItemTemplate" of type DataTemplate
, that the user can set.
Somewhere in my control template there's a CheckBox
and I'd like to use the ContentTemplateSelector
of that CheckBox
.
For that I wrote a class implementing DataTemplateSelector
, something like this:
Public Class TestTemplateSelector
Inherits DataTemplateSelector
Public Property Template1 As DataTemplate
Public Property Template2 As DataTemplate
Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
...
If condition Then
Return Me.Template1
Else
Return Me.Template2
End If
End Function
End Class
Now I'd like to instantiate and use this template selector in my control template and one of the templates of the selector should be the "ItemTemplate" from my custom control.
I can't do something like this
<DataTemplate x:Key="Template1">
<TextBlock Text="Template 1" />
</DataTemplate>
<local:TestTemplateSelector x:Name="TestTemplateSelector"
Template1="{StaticResource Template1}"
Template2="{Binding ItemTemplate, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:TestControl}}" />
because "Template2" is no dependency property.
So, how do I get the "ItemTemplate" from my custom control to the CLR property "Template2" of my template selector?
I can't turn "Template2" into a dependency property because "TestTemplateSelector" has to inherit from DataTemplateSelector
and can't inherit from DependencyObject
?