有一个变量SelfPath值在变化。当您单击主窗口上的按钮时,会弹出一个辅助窗口CreateFileWindow,您需要在其文本框中显示值SelfPath。也许这是微不足道的,但我是 C# 新手,我该如何引用这个变量?
namespace CommandExecutor
{
public partial class CreateFileWindow : Window
{
public string ViewModel { get; set; }
public CreateFileWindow()
{
InitializeComponent();
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
private void btnX_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void btnCreateFile_Click(object sender, RoutedEventArgs e)
{
FileDirTextBox.Text = "MainWindow.SelfPath ?";//здесь нужна переменная
}
}
public partial class MainWindow : Window
{
Process CmdProcess = new Process();
bool isWindowToggled = false;
public string SelfPath = Environment.CurrentDirectory;//переменная
修改子窗口的构造函数并添加一个包含对主窗口的引用的字段。
像这样使用
像这样创建一个子窗口