0

我有一个抽象基类aComponent和子类CriteriaListQuestion等等。

对于每个子类,我有两个对应UserControl的 s,它们的名称类似于ueCriteriaList, ubCriteriaList, ueQuestion,ubQuestion等(ue代表“UserControl带输入元素”和ubUserControl带按钮”的缩写。)

DataContext是“ControllerClass”,其属性my_aComponent为实际aComponent实例的 getter。当aComponent实例更改时(例如更改为 的实例CriteriaList),我想加载相应的 UserControls(在本例中为 ueCriteriaList 和 ubCriteriaList)。

我有两个转换器ueControlConverterubControlConverter它们采用类名(例如CriteriaList)并返回一个UserControl实例(在本例中为ueCriteriaList)。

Public Class ueControlConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As Type, parameter As Object,
                            culture As System.Globalization.CultureInfo) As Object
                           Implements IValueConverter.Convert
        Dim aComp As aComponent = value
        Dim assemblyKlassenname As String = aComp.GetType.ToString
        Dim assemblyName As String = Left(assemblyKlassenname,
                                          assemblyKlassenname.IndexOf(".") + 1)
        Dim klassenName As String = Right(assemblyKlassenname,
                                          assemblyKlassenname.IndexOf(".") - 1)
        Dim t As Type = Type.GetType(assemblyName & "ue" & klassenName)
        Dim o As UserControl = Activator.CreateInstance(t)
        o.DataContext = value
        Return o
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object,
                                culture As System.Globalization.CultureInfo) As Object
                    Implements IValueConverter.ConvertBack
        Return value
    End Function
End Class

在 XAML 中,我有两个ContentControl绑定Content="{Binding Path=my_aComponent, Converter={StaticResource _ueControlConverter}Content="{Binding Path=my_aComponent, Converter={StaticResource _ubControlConverter}. 权利UserControls 显示但没有绑定到my_aComponent

我能做些什么?

4

1 回答 1

0

您还需要通过绑定ContentControl对象来提供DataContext属性。

于 2013-07-15T19:53:56.213 回答