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" 
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                   xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"> 
    <Grid x:Name="LayoutRoot" Background="White"> 
        <Button Content="Close me!" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
    </Grid> 
</telerik:RadWindow> 

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!" 
        Click="Button_Click" 
        HorizontalAlignment="Center" 
        VerticalAlignment="Center" /> 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    this.Close(); 
} 
Private Sub Button_Click(sender As Object, e As RoutedEventArgs) 
    Me.Close() 
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!" 
                   HorizontalAlignment="Center" 
                   VerticalAlignment="Center" 
                   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>(); 
    window.Close(); 
} 
Private Sub Button_Click(sender As Object, e As RoutedEventArgs) 
    Dim window As RadWindow = Me.ParentOfType(Of RadWindow)() 
    window.Close() 
End Sub 

See Also

In this article