MAC
MAC

Reputation: 6577

add close button

How to add close button on the right top of the window in silverlight?

Upvotes: 0

Views: 2891

Answers (1)

Luke Baulch
Luke Baulch

Reputation: 3656

Assumptions:
1. You are wanting a close function using a control from within silverlight.
2. You are wanting the browser window to be closed..

Adding a button to your silverlight control:

<Button Margin="0,10,10,0" x:Name="CloseButton" VerticalAlignment="Top" HorizontalAlignment="Right" Content="Close" Click="CloseButton_Click" Width="75" Height="22" />

Adding the OnClick event:
If you are wanting to close the window, then you will need to execute some javascript in one way or another.

Solution 1:
You can add a javascript function on your html/aspx page like:

<script type="text/javascript">
    function CloseWindow()
    {
        window.close();
    }
</script>

and call it adding the OnClick event:

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.Invoke("CloseWindow");
}

Solution 2:
Alternatively you can execute the 'window.close()' using the HtmlPageWindow.Eval() method, like so without the need for a javascript function on the page:

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.Eval("window.close()");
}

Upvotes: 6

Related Questions