如何从窗口获取值我window.show();

时间:2010-11-07 09:50:15

标签: c# wpf wpf-4.0

我正在写一个简单的应用程序,当点击按钮时,命令被绑定,我正在做

 {...
newEvent.ExecuteTargets += exacuteNewEvent;
    }

    void exacuteNewEvent(string message)
    {
        Window1 w = new Window1();
        w.ShowDialog();

    }

我的问题是w如何调用我的主窗口知道它有一个新的nassage 我应该在w中插入一个他应该调用的窗口方法吗? 还有另一种方法吗?

1 个答案:

答案 0 :(得分:0)

传递对当前表单的引用,并使用它来调用它上面的函数。

class Form1 
{
    void Function() 
    {
        newEvent.ExecuteTargets += exacuteNewEvent;
    }

    void exacuteNewEvent(string message)
    {
        Window1 w = new Window1(this);
        w.ShowDialog();
    }

    public void ExecuteStuffInOtherWindow() 
    {
        // do something
    }
}

class Window1 
{
    Form _otherForm;

    public Window1(Form f) 
    {
        _otherForm = f;
        _otherForm.ExecuteStuffInOtherWindow(); // call code in other form
    }
}
相关问题