在用户控件中使用main_window中的方法 [英] using method from main_window in a usercontrol

查看:62
本文介绍了在用户控件中使用main_window中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


应用说明:

我有一个主要的wpf窗口,其中包含"add_button"和"view-list"按钮以及一个"content_grid".当用户单击"add_button"时,包含添加表单的控件将显示在"content_grid"中,单击时相同查看列表,他将获得包含列表+添加按钮的用户控件.

我遇到的问题:

为此,我在main_window show_control(control c)中有一个获取控件并将其显示在"content_grid"中的方法.问题是我要显示列表控件"中的"add_control"(用户单击列表控件"中的"add_btn"以显示"add_control").


预先谢谢您:)

Hi,
explanation of the application:

I have a main wpf windows containing "add_button" and "view-list" button and a "content_grid" .When the user clicks the "add_button" the control containing the add form is displayed in the "content_grid" , the same when he clicks view list he gets the usercontrol containing the list + add button.

The problem that i met:

To do that i have a method in my main_window show_control(control c) that gets the control and displays it in the "content_grid". the problem is that i want to display the "add_control" from the "list_control" (the user clicks "add_btn" that is in the "list_control" to display "add_control").


Thank you in advance :)

推荐答案

您好,

在用户控件中使用委托

例如,您在控件上有一个名为添加"的按钮:

Hello,

Use delegate in your User Control

For example you got a button named "Add" on your control:

public partial class Test : UserControl
{
    public Test()
    {
        InitializeComponent();
    }

    public delegate void RunMethodHandler(object sender);
    public event RunMethodHandler RunMethod;

    private void Add_Click(object sender, RoutedEventArgs e)
    {
        if (RunMethod != null)
            RunMethod(null);
    }
}




然后在您的MainWindow中,将UserControl添加到MainWindow.我做了,并将其重命名为"MyControl",然后:




Then in your MainWindow, Add the UserControl to MainWindow. I did and renamed it to "MyControl", then:

public MainWindow()
{
    InitializeComponent();

    MyControl.RunMethod +=new Test.RunMethodHandler(TestMethod);
}

public void TestMethod(object sender)
{
    MessageBox.Show("Ok");
}



最后,当您单击添加"时,将运行TestMethod().

祝你好运.



At last, when you Click on Add, the TestMethod() will run.

Good luck.


谢谢你的回答真的很有帮助:D
Thank you your answers were really helpful :D


这篇关于在用户控件中使用main_window中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆