我有一個代表WPF應用程序主窗口的XAML文件。 現在我希望這個窗口顯示由另一個XAML文件指定的內容。 這可行,但DataContext
丟失在我的UserControl
的C#代碼中。框架導航 - DataContext未繼承
我認爲<Frame Source=....>
以某種方式打破了WPF的邏輯樹。 我想要具有相同的行爲,就好像<Frame Source=....>
被Content1.xaml文件內容簡單地替換,即周圍的Window
類的DataContext
繼承到UserControl
。
有沒有簡單的方法來解決這個問題? 所有solutions,我發現似乎是矯枉過正。
僞
MainWindow.xaml
<Window ....>
<Frame Source="Content1.xaml" />
</Window>
Content1.xaml
<UserControl ....>
<!-- Content goes here -->
</UserControl>
看一看從喬·懷特這裏的響應http://stackoverflow.com/ question/3621424/page-datacontext-not-inherited-from-parent-frame – Michael
@Michael Joe White的解決方案適用於我。我想你可以將鏈接發佈爲這個問題的答案。 – kennyzx