0

I have an abstract base class aComponent and sub-classes CriteriaList, Question, etc.

For each sub-class I have two corresponding UserControls which have names like ueCriteriaList, ubCriteriaList, ueQuestion, ubQuestion, etc. (ue stands short for "UserControl with input elements", and ub for "UserControl with buttons".)

The DataContext is the "ControllerClass" with a property with my_aComponent as the getter for the actual aComponent instance. When the aComponent instance changes (for example to an instance of CriteriaList), I want to load the corresponding UserControls (in this case ueCriteriaList and ubCriteriaList).

I have two converters ueControlConverter and ubControlConverter which take the class name (e.g. CriteriaList) and return a UserControl instance (in this case, 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

In XAML I have two ContentControls which bind Content="{Binding Path=my_aComponent, Converter={StaticResource _ueControlConverter} and Content="{Binding Path=my_aComponent, Converter={StaticResource _ubControlConverter}. The right UserControls are shown but without binding to my_aComponent.

What can I do?

Ry-
  • 218,210
  • 55
  • 464
  • 476
PGoe
  • 11
  • 3
  • 1
    Tip: .NET is better than VB6! `Dim split As String = assemblyKlassenname.Split({"."c}, 2)` `Dim assemblyKlassenname As String = split(0)` `Dim klassenName As String = split(1)` – Ry- Jul 15 '13 at 20:12

1 Answers1

0

You need to also provide the DataContext property through binding for your ContentControl objects.

Avram Tudor
  • 1,468
  • 12
  • 18
  • How can I get it in the Converter and use it there? – PGoe Jul 16 '13 at 05:38
  • Hi @Potecaru Tudor, thanks for the answer. In my Converter I can see that the generated UserControl has the DataContext Null. This doesn't change after my "o.DataContext = value". How can I connect the generated UserControl-Object with my DataContext??? – PGoe Jul 16 '13 at 18:13
  • Hi @minitech, thanks for your edit - did you get my thanks when I write @minitech? I am new here and have to learn how formatting will work. And sorry for my English - I am from Germany. And respect, you seem to be very clever when you are invited to be an editor. – PGoe Jul 16 '13 at 18:17
  • This problem is solved. In my test-class I overlooked that there isn't a property but only a public variable for the ObservableCollection. The Converter is functioning. – PGoe Jul 17 '13 at 19:12