views:

430

answers:

2

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

A: 

Do you mean out-of-browser in SL3 or within Browser in SL3, or SL2 ?

dalind
This should be a comment, not an answer.
Bill the Lizard
+3  A: 

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()");
}
Luke Baulch