最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - Use binding to fill CLR property - Stack Overflow

programmeradmin0浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论