简体   繁体   English

C#WPF如何从已打开的UserControl中设置MainWindow.contentControl.Content

[英]C# WPF How to set MainWindow.contentControl.Content from already opened UserControl

How do I set the MainWindow.contentControl.Content from an already opened UserControl? 如何从已经打开的UserControl中设置MainWindow.contentControl.Content?

In my MainWindow I load an UserControl from where I want to override the MainWindow.contentControl.Content again but when I do so my whole menu disappears which is defined above the ContentControl :( 在我的MainWindow中,我从那里要再次覆盖MainWindow.contentControl.Content的位置加载一个UserControl,但是当我这样做时,整个菜单消失了,该菜单在ContentControl上方定义:

[CODE] MainWindow [CODE]主窗口

public MainWindow()
    {
        InitializeComponent();
        this.contentControl.Content = new MyUserControl();
    }

[XAML] MainWindow [XAML] MainWindow

<Grid>
    <DockPanel>
        <Menu IsMainMenu="True" DockPanel.Dock="Top">
            <MenuItem Header="_Application">
            </MenuItem>
        </Menu>
        <ContentControl x:Name="contentControl"/>
    </DockPanel>
</Grid>

[CODE] MyUserControl [CODE] MyUserControl

public MyUserControl()
    {
        InitializeComponent();
        Application.Current.MainWindow.Content = new MyUserControlNEW();
    }

When you assign Application.Current.MainWindow.Content you actually remove the root Grid of your window. 分配Application.Current.MainWindow.Content时,实际上是删除窗口的根Grid。

One solution is to find the ContentControl by name like this: 一种解决方案是按如下名称查找ContentControl:

(Application.Current.MainWindow.FindName("contentControl") as ContentControl).Content = new MyUserControlNEW();

But you really should avoid manipulating controls in this way for the sake of maintainability of your code. 但是,为了确保代码的可维护性,您实际上应该避免以这种方式操纵控件。 Try to implement MVVM pattern. 尝试实现MVVM模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将快捷方式从MainWindow传递给ContentControl中的UserControl? - How to pass shortcuts from MainWindow to the UserControl in ContentControl? 如何在WPF C#中以编程方式更改ContentControl中的UserControl - How to change UserControl inside of ContentControl in wpf c# programmatically c# WPF 在用户控制代码中无法识别主窗口按钮? - c# WPF Mainwindow button is not recognized in UserControl code? C#WPF userControl将数据发送到mainWindow textBlock on ButtonClick - C# WPF userControl to send data to mainWindow textBlock on buttonClick 如何同时单击位于一个MainWindow(C#/ Xaml)中的另一个UserControl来显示/调用另一个UserControl - How to diplay/call an UserControl from another UserControl at click both situated in one MainWindow (C#/Xaml) 如何使C#WPF ContentControl可序列化? - How to make C# WPF ContentControl Serializable? Contentcontrol.Content内存泄漏中的WPF用户控件 - WPF Usercontrol in Contentcontrol.Content Memory Leak 如何从 C# WPF 中的页面访问 MainWindow 变量? - How to access a MainWindow variable from a page in C# WPF? 如何从MainWindow C#WPF使用吸气剂 - How to use the getter from MainWindow C# WPF 如何在 MainWindow 的 ContentControl 中获取当前用户控件? (MVVM) - How to get the current usercontrol inside a ContentControl in MainWindow? (MVVM)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM