Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

WPF Popup window closed event

Environmental summary: I encountered an obstacle with my Visual Studio WPF (not Core) app. I created 2 different Windows, one of them is a MainWindow, where the software displays the data in ListBox with Binding, and the second one is a simply Add Data window.

In a MainWindow, if I click on "Add new element" button, it will show up the new window (mainWindow still displayed under), like this:

ModifyDetails details = new ModifyDetails(ListViewPositions.SelectedItem as Positions);
            details.Show();

If I finished with filling up my form I press the save button. It will update the Database, and close the window simply like this:

...Database updating...
this.Close();

So the problem is that I want to Refresh my ListBox after I close the "Add new element" window. I can Refresh it with a button on MainWindow, so the process is good, but I'm looking for an Closing event, or something to make it automatic.

Refresh process:

public void Refresh()
        {
            positions.Clear();
            positions = GetProducts();
            ListViewPositions.ItemsSource = positions;
            ListViewPositions.Items.Refresh();
        }
question from:https://stackoverflow.com/questions/65844801/wpf-popup-window-closed-event

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

use this code :

ModifyDetails details = new ModifyDetails(ListViewPositions.SelectedItem as Positions);
details.ShowDialog();
Refresh();

or:

ModifyDetails details = new ModifyDetails(ListViewPositions.SelectedItem as Positions, this);
details.Show();


//get popup window
CurrentWindow _window;
public ModifyDetails(position,window)
{
   _window = window;
}


...database updating...
//this method define a parent Window => public void Refresh(){//do work...}
_window.Refresh();
this.Close();

I will give a supplementary example

public partial class Window1 : Window
{
    public Window1()
    {
       InitializeComponent();
    }
    
    private void btnGoWindow2_Click(object sender, RoutedEventArgs e)
    {
        Window2 wnd = new Window2();
        wnd.Show(ListViewPositions.SelectedItem as Positions, this);
    }
    
    public void Refresh()
    {
       //update elements...
    }
}  

public partial class Window2 : Window
{
    Window1 window1;
    int position;
    public Window2(int _position, Window1 _window)
    {
       InitializeComponent();
       window1 = _window;
       position = _position;
    }
    
    //other method ....
    
    private void btnCloseWindow_Click(object sender, RoutedEventArgs e)
    {
        window1.Refresh();
        this.Close();
    }
}        

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...