I designed a UserControl and planning to display this as popup window when click a button in MainWindow .
I followed this Link. For open usercontrol as dialog window.
private void btnInstApp_Click(object sender, RoutedEventArgs e)
{
Window objWindow = new Window
{
Title = "Title 12345",
WindowStyle = WindowStyle.None,
WindowStartupLocation = WindowStartupLocation.CenterScreen,
AllowsTransparency=true,
Width = 500,
Height = 200,
Content = new ucInstrumentApp()
};
objWindow.ShowDialog();
}
I used None
as WindowStyle
. And designed a custom close button in UserControl for close the popup/dialog window. I tried the given below code. But it's not working.
private void btnClose_Click(object sender, RoutedEventArgs e)
{
//this.Close(); //:not working
Window objWindow = new Window
{
Content = new ucInstrumentApp()
};
objWindow.Close();
}
I am new to WPF/Windows forms. Can you guys guide me to solve this issue.
You need to get the parent Window
from your current UserControl
and then Close
it.
One solution to achieve such a thing could be the following one :
private void btnClose_Click(object sender, RoutedEventArgs e)
{
Window parentWindow = Window.GetWindow((DependencyObject)sender);
if (parentWindow != null)
{
parentWindow.Close();
}
}
As you can see, it's based on the Window.GetWindow method, here's its description :
Returns a reference to the Window object that hosts the content tree within which the dependency object is located.
try
private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
That should work :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With