2013-04-03 47 views
0

在我MainWindow.xaml頁的結果,我有以下的代碼工作(其中MyWord是一個字符串)DataTemplate中不顯示綁定兒童

<ContentControl Content="{Binding MyWord}" /> 

我玩的DataTemplates,並試圖瞭解他們。所以,我想從我的ContentControl中引用DataTemplateDataTemplate應該包含一個TextBlock,它綁定到我的字符串。我更新了我的代碼

<ContentControl ContentTemplate="{StaticResource ViewsTemplate}" /> 

在我的ResourceDictionary我添加

<DataTemplate x:Key="ViewsTemplate"> 
    <TextBlock Text="{Binding MyWord}" /> 
</DataTemplate> 

這都產生在屏幕上沒有文本。我甚至試過

<ContentControl Content="{Binding MyWord}" ContentTemplate="{StaticResource ViewsTemplate}" /> 

仍然沒有結果在屏幕上。

我無法弄清楚爲什麼有人可以給我一些建議。

謝謝

回答

1

ContentControl仍然需要綁定了一些內容。

<ContentControl ContentTemplate="{StaticResource ViewsTemplate}" Content="{Binding MyWord}" /> 

會的工作,但隨後你需要改變你的數據模板,因爲它希望能夠找到MyWord這當然就不能,所以你要使用只是{Binding}代替。

另外,綁定ContentControlContent{Binding} - 其母公司目前DataContext - 離開模板,因爲它是。

+0

謝謝馬修,這正是我所需要的。此外,非常感謝您提供這兩種方法,這些類型的建議通常非常有價值,非常感謝! – Dave

+0

我傾向於自己喜歡這些答案,所以我儘可能地做到這一點。您可能需要點擊大綠色的勾號來接受答案,並保持您的接受統計數據的高質量。 (免責聲明:如果你這樣做,我會獲得更多的聲譽:)) –