簡體   English   中英

WPF:用戶控件在設計時未在 ContentControl 的 Datatemplate 內顯示

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

我花了好幾天的時間試圖找出這里不起作用的地方,我懷疑這是 VS 中的一個錯誤,或者我監督的一些簡單的事情......

我有一個ContentControl提供視圖 model:

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

它綁定的屬性實際上是一組視圖模型的接口。

然后我為可能的ViewModels定義了DataTemplate

<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將只顯示一個純字符串,表示視圖 model 的路徑(例如“ MyApp.ViewModels.vmLearnSpeak ”)。

所有視圖模型都有默認構造函數來初始化一些虛擬數據,如果我在ViewModel中更改了具有ContentControl綁定屬性的虛擬初始化,那么 DataTemplate 也會在設計器中按預期更改(字符串更改為“ MyApp.ViewModels .vmLearnWrite ”例如)。

如果我用另一個控件(如按鈕)替換DataTemplate內容,如果設置了帶有按鈕的DataTemplate的相應Data ,則會顯示該按鈕。

所以總的來說, DataTemplate基本上是可以工作的。

自定義控件也可以工作:如果我將 DataTemplate 中的代碼直接放在布局中,則控件會按預期顯示。

因此,如果將控件放置在DataTemplate ...

我希望我提供了足夠的信息,否則請告訴我......提前感謝您的支持!

順便說一句:運行 Visual Studio 2016 社區,以防萬一可能存在已知錯誤(在花了這么多時間試圖修復之后,我不確定,但我相信它曾經在一段時間前工作過......)

編輯:

問題確實出在UserControl上,如果我添加一個簡單的空按鈕或只有一個按鈕UserControl ,也會出現同樣的問題。 但是,如果我放置CustomControl或常規Button ,則會顯示DataTemplate ...

僅供參考:顯然這是 Visual Studio 中已修復的錯誤,但我不確定何時發布:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM