Hi,@Karthick Arjunan. Welcome to Microsoft Q&A. The Window.GetWindow method is called with the button as the parameter, which correctly finds the window containing the button.
private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is DependencyObject depObj)
{
var window = Window.GetWindow(depObj);
MessageBox.Show(window?.ToString() ?? "Window not found");
}
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.