Search code examples
wpfvisual-studiouser-controlsdatatemplatecontentcontrol

WPF: UserControl not displaying at design time inside Datatemplate for a ContentControl


I spent quite some days trying to figure out what is not working here, and I suspect it is eighter a bug in VS or something plain simple I oversee...

I have a ContentControl which serves a view model:

<!-- Learning control from DataTemplate -->
<ContentControl Content     = "{Binding learningViewModel}"
                Grid.Row    = "1"
                Grid.Column = "1"
                Height      = "300"
                Margin      = "20, 0, 0, 0"/>

The property it is bound to is actually an interface to a set of view models.

Then I have a definition of DataTemplate for the possible ViewModels:

<Window.Resources>

    <DataTemplate DataType="{x:Type vm:vmLearnSpeak}">
        <local:viewLearnSpeak CommandNotKnown           = "{Binding DataContext.cmdNotKnown,                RelativeSource={RelativeSource AncestorType=local:wndSession}}"
                              CommandAlmostKnown        = "{Binding DataContext.cmdAlmostKnown,             RelativeSource={RelativeSource AncestorType=local:wndSession}}"
                              CommandKnown              = "{Binding DataContext.cmdKnown,                   RelativeSource={RelativeSource AncestorType=local:wndSession}}" />
    </DataTemplate>
    <DataTemplate DataType="{x:Type vm:vmLearnWrite}">
        <local:viewLearnWrite CommandNotKnown           = "{Binding DataContext.cmdNotKnown,                RelativeSource={RelativeSource AncestorType=local:wndSession}}"
                              CommandAlmostKnown        = "{Binding DataContext.cmdAlmostKnown,             RelativeSource={RelativeSource AncestorType=local:wndSession}}"
                              CommandKnown              = "{Binding DataContext.cmdKnown,                   RelativeSource={RelativeSource AncestorType=local:wndSession}}" />
    </DataTemplate>
    <DataTemplate DataType="{x:Type vm:vmLearnListen}">
        <local:viewLearnListen CommandNotKnown          = "{Binding DataContext.cmdNotKnown,                RelativeSource={RelativeSource AncestorType=local:wndSession}}"
                               CommandAlmostKnown       = "{Binding DataContext.cmdAlmostKnown,             RelativeSource={RelativeSource AncestorType=local:wndSession}}"
                               CommandKnown             = "{Binding DataContext.cmdKnown,                   RelativeSource={RelativeSource AncestorType=local:wndSession}}" />
    </DataTemplate>

</Window.Resources>

During programm execution the binding works as expected.

However during design time the ContentControl will only display a plain string representing the path of the view model (eg. "MyApp.ViewModels.vmLearnSpeak").

All view models have default constructors which initialize some dummy data and if I change that dummy initialization in the ViewModel that has the property the ContentControl is bound to, then the DataTemplate is also changed in designer as expected (the string changes to "MyApp.ViewModels.vmLearnWrite" for example).

If I replace the DataTemplate content by another control like a button, I get the button displayed if the correcponding Data for the DataTemplate with the button is set.

So in all, DataTemplate is basically working.

The custom controls work too: if I put the code from the DataTemplate directly in the layout, then the control appears as expected.

Hence it appears that the control will only fail to display during design time if placed inside a DataTemplate...

I hope I provided enough information, else let me know... Thanking you in advance for your support!

BTW: running Visual Studio 2016 communities, just in case there might be a known bug (After spending so much time trying to fix I am not sure but I believe that it used to work some time ago...)

EDIT:

The issue really is with the UserControl, if I add a simple empty or with only a button UserControl the same issue is there. Hoewver if i put a CustomControl or a regular Button, the DataTemplate is shown...


Solution

  • Just for information: apparently this was a bug in Visual studio that has been fixed, while I am not sure when it will release:

    https://developercommunity.visualstudio.com/content/problem/1004742/usercontrol-is-not-displayed-inside-datatemplate-i.html