Return an object from a popup window

前端 未结 6 467
面向向阳花
面向向阳花 2020-12-24 12:23

I have a Window which pop-ups another Window. I want the second Window to be able to return an object to the first Window when a button is pressed. How would I do this?

6条回答
  •  梦毁少年i
    2020-12-24 12:42

    I did it like that.

    In parent window make a corresponding ClosingEvent like this :

    private void NewWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            NewWindow nw = (NewWindow)sender;
    
            object Receiver = nw.Sender;
        }
    

    ...and in the child window change your code implement the "sender" like this :

        public partial class NewWindow : Window
    {
        public object Sender { get; set; }
    
        public NewWindow()
        {
            InitializeComponent();
    
            Sender = objectYouWantToSend;
        }
    }
    

    So when you put data into the object "Sender" in child window, while closing this window will put the data into the "Receiver" variable.

    I hope, this helps.

提交回复
热议问题