Closing a UserControl on Button Click in Silverlight


Closing a UserControl on Button Click in Silverlight

In this article, we are going to see how to close a user control by clicking a button inside it.

We had a scenario when using a telerik radgridview, we wanted to customize a filter for a date field. So we created a user control having 2 date pickers to select items within the range. But we are not able to close it. So I tried many things to close the user control, but we were not able to. Finally we came up with a solution after Googling for many hours which I am going to explain here. This is not only limited with customizing filter which I did it for my work, anyone can use this to close a UserControl in Silverlight by a button click.

2111.jpg

In my user control, I added a button as below in XAML.

<StackPanel Grid.Row="0" Grid.Column="3">

    <Button Style="{StaticResource CloseButton}" Width="13" Height="13" Margin="30,0,0,0" Padding="0,0,0,0"
          
 FontSize="9" Content="X" Click="Button_Click" x:Name="btnClose" VerticalContentAlignment
="Top" />

</StackPanel>


In the button click event, I have the following code which will close the user control:

private void Button_Click(object sender, RoutedEventArgs e)

{

        this.IsActive = false;

        var popup = btnClose.ParentOfType<System.Windows.Controls.Primitives.Popup>();

        if (popup != null)

        {

            popup.IsOpen = false;

        }

}


Similar Articles