ChildWindow.DialogResult Property

Silverlight

Gets or sets a value that indicates whether the ChildWindow was accepted or canceled.

Namespace:  System.Windows.Controls
Assembly:  System.Windows.Controls (in System.Windows.Controls.dll)

[TypeConverterAttribute(typeof(NullableBoolConverter))]
public Nullable<bool> DialogResult { get; set; }

Property Value

Type: System.Nullable<Boolean>
true if the child window was accepted; false if the child window was canceled. The default is null.

A ChildWindow is typically used as a dialog box and provides a DialogResult value. DialogResult can be used from the code that showed the ChildWindow to determine whether a user accepted (true) or canceled (false) the dialog box. If a dialog box was accepted, this signifies to the code that opened the child window to retrieve the data that was collected from the user and process it. If a dialog box was canceled, this signifies that the calling code should not process data from the child window.

Setting the DialogResult value automatically closes the child window. Closing the child window in any other way, including calling the Close method, pressing CTRL+SHIFT+F4, or clicking the close button, will set DialogResult to false. DialogResult is null as long as the child window is open.

To get the DialogResult value from a child window, handle the Closed event in the code-behind page of the calling window. In the Closed event handler, cast the sender parameter to a ChildWindow or a derived class to access the DialogResult property.

The following code example demonstrates how to set the DialogResult value in the click events of the OK and Cancel buttons of a ChildWindow. This example is part of a larger example available in the ChildWindow class overview.


private void OKButton_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = false;
}


The following example demonstrates how to get the DialogResult value in the Closed event handler. This code is in the page that called the ChildWindow. This example is part of a larger example available in the ChildWindow class overview.


void loginWnd_Closed(object sender, EventArgs e)
{
    LoginWindow lw = (LoginWindow)sender;

    if (lw.DialogResult == true && lw.nameBox.Text != string.Empty)
    {
        this.helloTxt.Text = "Hello " + lw.nameBox.Text;
    }
    else if (lw.DialogResult == false)
    {
        this.helloTxt.Text = "Login canceled.";
    }
}


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show: