2015-10-05 53 views
1

我在WPF中有一個MainWindow。如果我點擊一個按鈕,則執行此代碼:在高級設置中保存過濾器

private void buttonAdvSett_Click(object sender, RoutedEventArgs e) 
{ 
    AdvancedSettingsWindow advancedSettingsPopup = new AdvancedSettingsWindow(); 
    advancedSettingsPopup.ShowDialog(); 
} 

將打開一個新窗口。在這個窗口中,我設置了一些過濾器(通過ComboBoxes)。如果我點擊一個服裝「確定」按鈕,我想從組合框中保存字符串。

private void button_Click(object sender, RoutedEventArgs e) 
{ 
    // save the strings from the comboboxes if I click OK and close the window 
    this.Close(); 
} 

我希望你能幫助我。 對不起,我的英語不好。英語不是我的母語。

祝您有美好的一天。

envy6

UPDATE:

這也爲我工作: 在AdvancedSettingsWindow:

private void button_Click(object sender, RoutedEventArgs e) 
     { 
      Foo(); 
      this.Close(); 
     } 

public event Action<string> Check; 

public void Foo() 
     { 
      if(Check != null) 
      { 
       Check(methodINeedInMyMainWindow()); 
      } 
     } 

在我的主窗口:

private void buttonAdvSett_Click(object sender, RoutedEventArgs e) 
     { 

      AdvancedSettingsWindow advancedSettingsPopup = new AdvancedSettingsWindow(); 
      advancedSettingsPopup.Check += value => labelCurrentFilterText.Content = value; 
      advancedSettingsPopup.ShowDialog(); 
     } 

來源:C# - Return variable from child window to parent window in WPF

+0

爲什麼不公開這些值作爲窗口的屬性?您可以在窗口關閉後訪問它們。如果窗口方式通過「確定」按鈕或「取消」等關閉,您也可以存儲。 – Onur

+0

_save_是什麼意思?你是否想將它保存到內存中,這樣你就可以在應用程序的任何地方使用它,或者你想將它保存到一個文件中,以便在應用程序啓動時加載它? – Pieter

回答

0

MainWindow按鈕的方法應該看起來像:

private void button_Click(object sender, RoutedEventArgs e) 
    { 
     AdvancedSettingsWindow newPopupWindow = new AdvancedSettingsWindow(); 
     newPopupWindow.ShowDialog(); 
     if(newPopupWindow.DialogResult == true && newPopupWindow.TestCheckBox.IsChecked == true) 
     { 
      string str = newPopupWindow.TestCheckBox.Content.ToString(); 
      //str contains the text in the checkbox 
     } 

    } 

AdvancedSettingsWindow OK按鈕應該是這樣的:

private void button_Click(object sender, RoutedEventArgs e) 
    { 
     DialogResult = true; 
     Close(); 
    } 

顯然,這假設你只有一個複選框,所以你必須稍微擴展一下這個方法,但是這比使用公有屬性更有效率。

+0

謝謝!這對我來說可以! :-) – envy6