Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom Close Button in WPF

I need to implement a custom window decorator where a Close button will behave exactly as the close or x button on all Windows applications.

like image 666
kumarharsh Avatar asked Mar 04 '11 12:03

kumarharsh


2 Answers

Simply call the close() function from your button:

WPF:

<Button Name="CloseButton" Content="x" Click="CloseButton_Click" />

code-behind:

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
    Close();
}
like image 190
Martin Hennings Avatar answered Nov 06 '22 03:11

Martin Hennings


If you would like to use MVVM architecture, then you can pass the name of your window as a Command Parameter and in the Command you can close the window.

The code would be something like this:

Button Command="{Binding MainCloseButtonCommand}"
CommandParameter="{Binding ElementName=mainWindow}"

private void performMainCloseButtonCommand(object Parameter)
{
    Window objWindow  = Parameter as Window;
    objWindow.Close();
}
like image 2
Paras Avatar answered Nov 06 '22 05:11

Paras