Window.GetWindow() of TabItems content does not return the right window when TabItems State changes

Karthick Arjunan 0 Reputation points
2024-08-02T05:27:50.5833333+00:00

When using TabControl in two windows, each TabItem contains a button with a click event that displays the parent window of the TabItem using Window.GetWindow(). After DragAndDrop a TabItem from one window to another, GetWindow() still returns the previously docked window only. We have attached the sample and demo video for reference in the below mentioned link.

Link: SampleAndDemoVideo

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,823 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,765 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,887 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 1,690 Reputation points Microsoft Vendor
    2024-08-02T06:48:19.81+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.