如何从主GUI打开新窗口并将变量传递给新窗口

时间:2012-03-05 18:18:53

标签: c# wpf

我有一个带有文件和菜单的列表框的应用程序。当我右键单击列表框中的项目时,我有一个菜单,例如发送。当我按下'发送'时,我想要打开另一个窗口(我已经有了新窗口),在新窗口中我想要选择我选择的项目路径(我在主窗口中有这个路径)。

private void MenuItemSend_Click(object sender, RoutedEventArgs e) 
{             
    if (listBoxFiles.SelectedIndex == -1) 
    { 
        return; 
    } 

    string filePath = (listBoxFiles.SelectedItem).ToString(); --- my file path
    StatisticsWindow sForm = new StatisticsWindow();
    sForm.ShowDialog(); -- open the new window
} 

我该怎么做?

由于

1 个答案:

答案 0 :(得分:4)

为什么不为窗口创建构造函数?

而不是

new IpStatisticsWindow();

这样:

new IpStatisticsWindow(filePath);
// In the IpStatisticsWindow class
public IpStatisticsWindow(string path)
{
    //do something with path
}

你当然也可以创建一个处理它的属性或方法,然后你可以将它传递给那里,例如。

IPsForm.Path = filePath;
IPsForm.HandlePath(filePath);