Jonathan Allen
Jonathan Allen

Reputation: 70307

Silverlight: Closing a ChildWindow when the Overlay is clicked

If the user clicks on the overlay, I want the ChildWindow to automatically close and return the user to the main screen.

Is there a property that controls this? If not, is there a way to attach a click handler to the overlay?

Upvotes: 1

Views: 501

Answers (1)

Jonathan Allen
Jonathan Allen

Reputation: 70307

Turns out you can get a reference to the overlay right after it is created. After that it is a simple matter of attaching the event handler.

    private void Overlay_MouseButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.Close();
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        var overlay = (Grid)GetTemplateChild("Overlay");
        overlay.MouseLeftButtonDown += Overlay_MouseButtonDown;
        overlay.MouseRightButtonDown += Overlay_MouseButtonDown;
    }

Upvotes: 4

Related Questions