从当前窗口关闭上一个窗口

时间:2012-05-10 14:18:47

标签: c# silverlight childwindow

在Silverlight中,我使用的是配置文件窗口,可以选择删除超链接按钮中的当前配置文件。当用户点击超链接按钮时,它会将他们带到新表单以确认删除。虽然删除功能有效(即配置文件已从数据库中删除),但如何在用户确认时关闭第一个窗口?

这是超链接调用的内容 -

private void deleteProfile(object sender, RoutedEventArgs e)
    {

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

然后从dProfile开始,我想关闭用户点击确定时超链接所在的profileForm -

private void OKButton_Click(object sender, RoutedEventArgs e)
    {

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

        Close();
    }

但是dProfile表单在我创建新实例时才识别profileForm,如何引用我正在使用的当前实例?

2 个答案:

答案 0 :(得分:2)

可能还有其他一些方法,但您可以尝试以下方法。

在子窗口中创建一个事件

public event EventHandler SubmitClicked;
在你的OKButton_Click事件中

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

主窗口中执行以下操作,针对ChildWindow对象附加事件

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

然后你可以这样做:

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

(虽然这里不需要,但您可以使用该过程将值从子窗口传递到父窗口)同时查看此article

答案 1 :(得分:0)

使自己成为删除表单上的属性。 该属性应为:IsDeleted。 用户单击“确定”时将其设置为True。 然后当您返回主窗口时,请阅读此属性并关闭当前窗口。

或者从子窗口中获取结果......

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

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