You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现从UserControl触发主窗口切换显示另一个UserControl?

WPF UserControl切换问题求助!

各位前辈好,我刚接触编程没多久,现在在做一个WPF小项目,卡在UserControl切换的功能上好几个小时了,查了好多资料还是没搞定,想请大家帮我看看哪里出问题了😭

我的需求很明确:

  • 程序启动时,先显示一个作为主菜单的UserControl,里面有「新游戏」「加载游戏」「设置」三个按钮
  • 点击「新游戏」按钮的时候,要把当前的主菜单替换成另一个游戏界面的UserControl

下面是我目前写的代码,麻烦大家帮我排查下问题:


主窗口MainWindow.xaml

<Window x:Class="GameDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GameDemo"
        mc:Ignorable="d"
        Title="GameDemo" Height="600" Width="800">
    <Grid>
        <!-- 我用ContentControl来放要显示的UserControl -->
        <ContentControl x:Name="MainContentArea"/>
    </Grid>
</Window>

主窗口后台MainWindow.xaml.cs

using System.Windows;

namespace GameDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            // 启动时加载主菜单
            MainContentArea.Content = new MainMenuUC();
        }
    }
}

主菜单UserControl(MainMenuUC.xaml)

<UserControl x:Class="GameDemo.MainMenuUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:GameDemo"
             mc:Ignorable="d" 
             d:DesignHeight="600" d:DesignWidth="800">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="20">
        <Button Content="新游戏" Width="150" Height="50" FontSize="16" Click="NewGameBtn_Click"/>
        <Button Content="加载游戏" Width="150" Height="50" FontSize="16"/>
        <Button Content="设置" Width="150" Height="50" FontSize="16"/>
    </StackPanel>
</UserControl>

主菜单后台MainMenuUC.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace GameDemo
{
    public partial class MainMenuUC : UserControl
    {
        public MainMenuUC()
        {
            InitializeComponent();
        }

        private void NewGameBtn_Click(object sender, RoutedEventArgs e)
        {
            // 这里是我尝试切换的代码,但点击后没反应
            var mainWindow = (MainWindow)Window.GetWindow(this);
            mainWindow.MainContentArea.Content = new GamePlayUC();
        }
    }
}

游戏界面UserControl(GamePlayUC.xaml,就是个简单测试界面)

<UserControl x:Class="GameDemo.GamePlayUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:GameDemo"
             mc:Ignorable="d" 
             d:DesignHeight="600" d:DesignWidth="800">
    <Grid Background="#FFE0F7FA">
        <TextBlock Text="欢迎进入游戏!" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</UserControl>

我按照查到的方法,在按钮点击事件里获取主窗口实例,然后替换ContentControl的Content,但点击「新游戏」按钮后界面完全没变化,主菜单还在那里。我也试过用MVVM绑定CurrentView的方式,结果也是一样没反应。作为编程新手,实在找不到哪里错了,麻烦大家帮我看看问题出在哪,谢谢啦!

内容的提问来源于stack exchange,提问作者P. Ash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:37:29