2011-11-21 148 views
0

在MainWindow.xaml.cs中我想打開另一個窗口。我的代碼是這樣的:C#通過單擊另一個窗口的按鈕打開一個窗口

WordToHtml.Window1 newForm = new WordToHtml.Window1(); 
newForm.Show(); 
return newForm.imagePath; 

在Window1.xaml我有標籤,按鈕和文本框。我希望用戶點擊按鈕,然後從文本框中讀取內容。但是,當斷點指向newForm.Show()時,Window1將顯示但不可見。它沒有標籤/按鈕等。這是Window1的代碼:

string imagePath; 
public Window1() { 
    InitializeComponent(); 
    label1.Content = @"Please enter path"; 
} 
void button1_Click(object sender, RoutedEventArgs e) { 
    //this is never entered because I can't see the button 
} 
public string newImagePath(string imagePath) { 
    return imagePath; 
} 
+0

你在哪裏插入斷點?嘗試在button_click中插入它。 – Muse

+0

當我知道它不會被擊中時,爲什麼我會在button_click中插入斷點。我看不到按鈕,所以我不能點擊它。 –

+0

你可以發佈你的XAML嗎?所以如果我理解正確,一個新的窗口實際上是打開的,但它沒有任何內容? – Peter

回答

1

你所顯示的代碼片段並沒有讓事情變得清晰。我指出了我的困惑&問題。

1)。我假設當用戶點擊MainWindow上的某個按鈕時,新窗口應該打開「Window1」,其中有一個標籤,按鈕和文本框。然後用戶在文本框中輸入一些路徑並單擊window1應該關閉的按鈕,並且圖像路徑應該以「MainWindow」形式提供。如果我錯了,請糾正我。 2)。在此代碼段

WordToHtml.Window1 newForm = new WordToHtml.Window1(); 
    newForm.Show(); 
    return newForm.imagePath; 

在「newForm.imagePath」或空或空字符串你不會得到任何東西當你試圖在用戶輸入在文本框中的任何值之前訪問此。 3)。使用「SomeForm.Show()」方法將打開非模態對話框的新表單意味着用戶仍然可以獲得「MainWindow」的焦點或單擊按鈕(即從中打開新的Window1)。我建議使用「newForm.ShowDialog()」窗口,該窗口僅在關閉時纔將焦點返回給父窗口。 4)。您shold使用

newForm.Closing事件獲得新形式的參考,在關閉之前,你可以找到TextBox控件

string imagePath = (newForm.FindName("nameOfTextBox") as TextBox).Content.ToString(); 

,並獲得在主窗口中的ImagePath。

相關問題