![](/img/trans.png)
[英]How to pass shortcuts from MainWindow to the UserControl in ContentControl?
[英]C# WPF How to set MainWindow.contentControl.Content from already opened UserControl
如何從已經打開的UserControl中設置MainWindow.contentControl.Content?
在我的MainWindow中,我從那里要再次覆蓋MainWindow.contentControl.Content的位置加載一個UserControl,但是當我這樣做時,整個菜單消失了,該菜單在ContentControl上方定義:
[CODE]主窗口
public MainWindow()
{
InitializeComponent();
this.contentControl.Content = new MyUserControl();
}
[XAML] MainWindow
<Grid>
<DockPanel>
<Menu IsMainMenu="True" DockPanel.Dock="Top">
<MenuItem Header="_Application">
</MenuItem>
</Menu>
<ContentControl x:Name="contentControl"/>
</DockPanel>
</Grid>
[CODE] MyUserControl
public MyUserControl()
{
InitializeComponent();
Application.Current.MainWindow.Content = new MyUserControlNEW();
}
分配Application.Current.MainWindow.Content時,實際上是刪除窗口的根Grid。
一種解決方案是按如下名稱查找ContentControl:
(Application.Current.MainWindow.FindName("contentControl") as ContentControl).Content = new MyUserControlNEW();
但是,為了確保代碼的可維護性,您實際上應該避免以這種方式操縱控件。 嘗試實現MVVM模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.