ajita srivastava
ajita srivastava

Reputation: 45

Identifying if a window instance is already opened

I am building a WPF chat application. I am opening a new chat window when the user clicks on the username from chat list. If the user clicks on another username in chat list then a new instance of the chat window gets opened.The problem here is how can I check whether that user chat window is already opened to append the incoming chats. Is there any unique id associated with each window?If yes,then how can I check whether that particular window is opened or not.

Code to create new instance of chat window when user clicks on username:

private void UsersChatWith_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (UsersChatWith.SelectedItem != null)
    {
         var SelItm = UsersChatWith.SelectedItem;
         ChatToUserid = ((UserDetail)(SelItm)).UserId;
         ChatToUserName = ((UserDetail)(SelItm)).User_FirstName;
    }
    //  UserPersonalChatPopUp.IsOpen = true;
    UserPersonalChatWindow chatwin = new UserPersonalChatWindow();
    chatwin.Title = ChatToUserName;
    chatwin.StaffUserId.Content = ChatToUserid;
    chatwin.Show();
}

Here each time I click on any user then new chat window gets opened.I need to know the condition which i can check before creating a new instance of the chat window(if not already created and opened).Please suggest.

Upvotes: 1

Views: 133

Answers (1)

Sievajet
Sievajet

Reputation: 3533

You can iterate over the current windows to determine if one exists or not.

var window = Application.Current.Windows.OfType<UserPersonalChatWindow>()
.FirstOrDefault(x => x.StaffUserId.Content == id);

Upvotes: 3

Related Questions