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

标签 c# wpf xaml user-controls

我有 MainWindow.xaml,其中包含 ContentControl。 我创建了 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 的内容。

最佳答案

针对您的场景使用委托(delegate)和事件。 发布事件 main_screen.xaml.cs 并在 MainWindow.xaml.cs 中订阅该事件

发布事件

ma​​in_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();
    }
}

关于c# - 如何以编程方式更改 wpf c# 中 ContentControl 内部的 UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29597773/

相关文章:

c# - 如何运行 TestClass 的所有单元测试及其内部 TestClass 测试?

c# - 使用 ComplexType 参数通过 WCF webHttpBinding 上传文件

java - 在无符号字节变量中表示有符号字节

c# - 元组的替代方法(setter)

c# - NUnit 确实需要 "magic strings"而 MSTest 不需要吗?

c# - 如何解析 MarkupExtension 中数据绑定(bind)的值?

.net - 在 WPF 中画一个十字

c# - ASP.Net MVC 中模型启动的数据绑定(bind)(更新 UI)

c# - 我的 XAML 有什么问题?

C# WPF Datagrid 如何禁用特定列上的选择单元格