如何以编程方式更改 wpf c# 中 ContentControl 内部的 UserControl

How to change UserControl inside of ContentControl in wpf c# programmatically

我有 MainWindow.xamlContentControl。 我创建了 4 个 UserControls。 我想在 UserControl 中按下按钮时更改 MainWindow.xamlContentControl 的内容。 这是我的 MainWindow.xaml:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:KIOSK" mc:Ignorable="d" x:Class="KIOSK.MainWindow"
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
    <ContentControl Name="contentMain">
        <local:main_screen />
    </ContentControl>
</Window>

这是我的用户控件:

1)main_screen.xaml

<UserControl x:Class="KIOSK.main_screen"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Name="grid1" ShowGridLines="True">            
        <Button Margin="10" Background="#FFA4F200" Click="Button_Click"/>                        
    </Grid>
</UserControl>

2) ClubRules.xaml

<UserControl x:Class="KIOSK.ClubRules"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Background="White">
    <Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="grid1">            

        <Button Margin="0,15,0,15" Background="#FFFE0555" HorizontalAlignment="Center" Click="Button_Click" />                           
    </Grid>
</UserControl>

我在 main_creen.xaml.cs 中为按下的按钮写的:

ClubRules cr = new ClubRules();
MainWindow mw = new MainWindow();
mw.contentMain.Content = new ClubRules();

但是它不起作用.. 我想在按下按钮时更改 UserControl 中 ContentControl 的内容。

为您的方案使用委托和事件。 发布活动 main_screen.xaml.cs 并在 MainWindow.xaml.cs

中订阅活动

发布活动

main_screen.xaml.cs

public partial class main_screen: UserControl
{
    public Delegate del;
    public main_screen()
    {
        InitializeComponent();
    }
    public void method1()
    {
        del.DynamicInvoke();
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        method1();
    }
}

在 MainWindow.xaml.cs

中订阅该活动

MainWindow.Xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:KIOSK" mc:Ignorable="d" x:Class="KIOSK.MainWindow"
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
    <ContentControl Name="contentMain">
        <local:main_screen x:Name="main_screen_obj" />
    </ContentControl>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public delegate void ValuePassDelegate();
    public event ValuePassDelegate ValuePassEvent;

    public MainWindow()
    {
        InitializeComponent();
        ValuePassEvent += new ValuePassDelegate(method1);
        main_screen_obj.del = ValuePassEvent;
    }
    public void method1()
    {
        contentMain.Content = new ClubRules();
    }
}

您正在创建一个 new MainWindow() 而不是使用正在显示的那个。您应该将 ClubRules 分配给正在显示的 Content

一种方法是将按钮从 UserControl 移到 MainWindow 本身。 @decoherence 建议的其他方法是使用 singleton pattern.

可能还有更多方法,但您基本上需要使用已显示的 MainWindow 的相同实例。