Question

I have encountered an error called System.InvalidOperationException.

Additional information: Specified element is already the logical child of another element. Disconnect it first.

I have searched through internet for the solutions but none of them solve my problem.

I created a button called "Open Dialog", to open a dialog. After I opened the dialog, there are two buttons showed in the dialog, "Save" and "Cancel". The function of "Cancel" is to close the dialog. However, after I clicked "Cancel", when I tried to reopen the dialog again by clicking the "Open Dialog", I have encountered the error stated above.

I will post my codes here as well for the event handling method.

In the class of MainWindow:

private void openDialogButton_Click(object sender, RoutedEventArgs e)
        {
            PersonIDTable.ShowDialog();
        }

In the ViewModel:

public void ShowDialog()
        {
            PersonID.UserControls.PersonIDDialog dialog = new PersonID.UserControls.PersonIDDialog (this);
            dialog.Show();
        }

In the class of Dialog:

private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

Do you guys have any idea to solve the problem? Your help will be much appreciated.

Était-ce utile?

La solution

You pretty much told the answer in your question, you closed the dialog so you can't reopen it. Insert the creation of PersonIDTable inside openDialogButton_Click, that should fix it.

Example:

Form1 form = new Form1();
form.ShowDialog();

Would have told you this through the comment, but I don't have enough reputation.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top