WPF: UserControl在设计时不显示在一个ContentControl的Datatemplate中。

问题描述 投票:0回答:1

我花了相当多的日子试图找出什么是不工作在这里,我怀疑它是在VS中的一个错误或一些简单的东西,我监督......

我有一个 ContentControl 它服务于一个视图模型。

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

它所绑定的属性实际上是一组视图模型的接口。

然后我有一个定义 DataTemplate 为可能的 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>

在程序执行过程中,绑定按预期工作。

但在设计时 ContentControl 将只显示一个代表视图模型路径的普通字符串(例如:"MyApp.ViewModels.vmLearnSpeak").

所有的视图模型都有默认的构造函数,这些构造函数初始化了一些虚数据,如果我改变了在 ViewModel 具有 ContentControl 的绑定,那么DataTemplate也会按照预期在designer中进行更改(字符串变为"MyApp.ViewModels.vmLearnWrite例如"")。)

如果我把 DataTemplate 控件的内容,如按钮,我得到的按钮显示,如果相关的 Data 对于 DataTemplate 与按钮的设置。

所以在所有。DataTemplate 基本上是可行的。

自定义控件也能正常工作:如果我把DataTemplate的代码直接放在布局中,那么控件就会按照预期出现。

因此,似乎只有在设计时,如果将控件放在一个叫 "数据模板 "的地方,控件才会无法显示。DataTemplate...

希望我提供的信息足够多,否则请告诉我... ... 提前感谢您的支持!

另外:运行Visual Studio 2016社区,以防万一可能有已知的bug(花了这么多时间试图修复,我也不确定,但我相信前段时间曾经用过......)。

EDIT:

这个问题确实是与 UserControl如果我添加一个简单的空的或只有一个按钮。UserControl 同样的问题是存在的。但如果我把一个 CustomControl 或普通 Buttonజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజ DataTemplate 是显示...

wpf visual-studio user-controls datatemplate contentcontrol
1个回答
0
投票

仅供参考:显然这是Visual studio中的一个bug,已经被修复了,而我不确定何时会发布。

https:/developercommunity.visualstudio.comcontentproblem1004742usercontrol-is-not-display-inside-datatemplate-i.html。

© www.soinside.com 2019 - 2024. All rights reserved.