Close previous window from current window

杀马特。学长 韩版系。学妹 提交于 2019-12-11 10:19:01

问题


In Silverlight I am using a profile window, with the option to delete the current profile in a hyperlink button. When the user hits the hyperlink button it takes them to a new form to confirm the deletion. Although the delete function has worked (i.e. the profile has been deleted form the datatbase), how can I get the first window to close when the user confirms?

This is what the hyperlink calls-

private void deleteProfile(object sender, RoutedEventArgs e)
    {

        deleteProfile dProfile = new deleteProfile();
        dProfile.textBlock1.Text = System.Convert.ToString("Delete " + lblMsg.Content);
        dProfile.Show();
    }

Then from there on dProfile I want to close the profileForm that the hyperlink sits on when the user clicks ok-

private void OKButton_Click(object sender, RoutedEventArgs e)
    {

        textBlock1.Text = "Profile Deleted.";
        profileForm.Close();

        Close();
    }

However the dProfile form only recognises profileForm when I create a new instance of it, how can I reference the current one I am using?


回答1:


There may be some other way, but you can try the following.

Create an event in Child Window

public event EventHandler SubmitClicked;

in your OKButton_Click event

private void OKButton_Click(object sender, RoutedEventArgs e)
{
    if (SubmitClicked != null)
    {
        SubmitClicked(this, new EventArgs());
    }
}

Do the following in the main window, attach event against the ChildWindow object

deleteProfile.SubmitClicked += new EventHandler(deleteProfile _SubmitClicked);

Then in the event you could do:

private void deleteProfile_SubmitClicked(object sender, EventArgs e)
{
    this.Close();
}

(Although its not required here, but you can use the process to pass values from Child window to parent window) Also check out this article




回答2:


Make your self a property on the Delete Form. This property shall be: IsDeleted. Set it to True when the user clicks OK. Then when you return to your main window, read this property and close the current window.

Or get the result from the child window...

var viewModel = _srvLocator.GetInstance<YourViewModel>();
var inst = _srvLocator.GetInstance<IWindowManager>();

if (inst.ShowDialog(viewModel).Value)
{
Close();
}


来源:https://stackoverflow.com/questions/10535987/close-previous-window-from-current-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!