从子窗口上的按钮单击事件更新主页上的列表框。

时间:2012-05-10 15:37:48

标签: c# silverlight listbox refresh childwindow

我有一个包含listBox的主页面。

当用户从列表框中选择配置文件时,会打开一个名为pWindow的子窗口。 此窗口作为通过超链接按钮删除当前配置文件的选项,该按钮打开另一个名为dprofile的确认窗口。

我的问题是,一旦用户确认删除了他们所在的当前个人资料,并在单击dProfile的按钮中确认,我怎么能更新第一个主页面中的listBox所以该列表不再包含已删除的配置文件(目前它没有这样做。

dProfile窗口中,我创建了一个事件 -

public event EventHandler SubmitClicked;

在确定按钮中单击我有 -

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

所以在我添加的主页面上 -

private void deleteProfile_SubmitClicked(object sender, EventArgs e)
    {
        WebService.Service1SoapClient client = new WebService.Service1SoapClient();

        listBox1.Items.Clear();
        client.profileListCompleted += new EventHandler<profileListCompletedEventArgs>(client_profileListCompleted);
        client.profileListAsync(ID);
    }

我认为这可能已经更新了listBox,因为它已在dProfile表单中确认,但是当表单关闭时,listBox保持不变,我必须手动刷新网页才能看到更新。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

如果我理解正确,那么你有三页。主要,pWindow和dProfile。 Earlier你试图从dProfile中关闭pWindwow并且工作正常。现在您要刷新主页面上的listBox1。
要实现这一点,您可以遵循类似的策略。您可能正在从Main页面打开pWindow,其中包含以下行中的内容

pWindow pWin = new pWindow();
pWin.Show();

现在您可以在pWindow类中定义一个新事件。

public event EventHandler pWindowRefeshListBox;

然后在deleteProfile_SubmitClicked的事件处理程序中,您可以将事件提升到刷新listbox1,如下所示:

private void deleteProfile_SubmitClicked(object sender, EventArgs e)
{
    if(pWindowRefreshListBox != null)
        pWindowRefreshListBox(this, new EventArgs());
    this.Close();
}

然后在主页面中注册针对pWin对象的事件,该对象是您之前定义的。

pWin.pWindowRefreshListBox += new new EventHandler(pWindow_pWindowRefreshListBox);

然后在主页面中定义事件。

private void pWindow_pWindowRefreshListBox(object sender, EventArgs e)
{
    listBox1.Items.Clear();
}

这应该刷新列表框。我没有测试代码或语法。所以你可以检查一下 在实施之前。

修改
您可以将dProfile中的事件定义为静态

public static event EventHandler SubmitClicked;

然后,您将能够在Main和pWindow中根据类名

注册它
dProfile.SubmitClicked += new ..............

然后在pWindow中相应地实现它,关闭窗口并在主刷新列表框中

修改
您可以在主页面上创建deleteProfile实例,在主

中注册以下内容
deleteProfile.SubmitClicked += new EventHandler(deleteProfile _SubmitClicked)

这应该有效