New to Telerik UI for WPF? Download free 30-day trial

Close the RadWindow Through its Content

To close the RadWindow through its content you have to get the instance of the RadWindow and call its Close() method. This tutorial will handle two possible scenarios:

Content, directly set to the RadWindow

To learn more about how to use the RadWindow as user control read this topic.

Here is a sample RadWindow, used as user control, with a button in it:

<telerik:RadWindow x:Class="RadWindowSamples.RadWindowControl" 
    <Grid x:Name="LayoutRoot" Background="White"> 
        <Button Content="Close me!" HorizontalAlignment="Center" VerticalAlignment="Center" /> 

As the button is in the same control like the RadWindow, you can get the RadWindow instance in the code-behind. In order to close the window through the button you have to attach to its Click event and call the Close() method of the RadWindow.

<Button Content="Close me!" 
        VerticalAlignment="Center" /> 

private void Button_Click(object sender, RoutedEventArgs e) 
Private Sub Button_Click(sender As Object, e As RoutedEventArgs) 
End Sub 

Instead of event handler you can use a RadButton with the Close command for the RadWindow. Here is an example:

<telerik:RadButton Content="Close me!" 
                   Command="telerik:WindowCommands.Close" /> 

Content, represented by a UserControl

In this scenario the Button is placed inside a UserControl, which is passed as the Content of the RadWindow. In this case you cannot access the RadWindow directly from the UserControl as it is not aware of its existance. In order to close the RadWindow when the button is clicked, you have to use the ParentOfType<T>() extension method to get the RadWindow instance.

The ParentOfType<T>() is available when you use the Telerik.Windows.Controls namespace from the Telerik.Windows.Controls. It returns the first element of this type up in the Visual Tree.

private void Button_Click(object sender, RoutedEventArgs e) 
    RadWindow window = this.ParentOfType<RadWindow>(); 
Private Sub Button_Click(sender As Object, e As RoutedEventArgs) 
    Dim window As RadWindow = Me.ParentOfType(Of RadWindow)() 
End Sub 

See Also

In this article