我有一个抽象基类aComponent
和子类CriteriaList
,Question
等等。
对于每个子类,我有两个对应UserControl
的 s,它们的名称类似于ueCriteriaList
, ubCriteriaList
, ueQuestion
,ubQuestion
等(ue
代表“UserControl
带输入元素”和ub
“UserControl
带按钮”的缩写。)
这DataContext
是“ControllerClass”,其属性my_aComponent
为实际aComponent
实例的 getter。当aComponent
实例更改时(例如更改为 的实例CriteriaList
),我想加载相应的 UserControls(在本例中为 ueCriteriaList 和 ubCriteriaList)。
我有两个转换器ueControlConverter
,ubControlConverter
它们采用类名(例如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}
. 权利UserControl
s 显示但没有绑定到my_aComponent
。
我能做些什么?