Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7750659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:20:52+00:00 2026-06-01T11:20:52+00:00

I have a window, and its datacontext is a viewmodel. now, outside the scope

  • 0

I have a window, and its datacontext is a viewmodel. now, outside the scope of the vm and window(view), i have a method that will be called. the method needs to show the window and let the user change some things, then when the user is done, the window needs to close and the method continue. tricky thing is, i have buttons on the window bound to commands in the viewmodel. these commands fire events like ‘userCancelled’ and ‘userOked’. so i want to catch these events to know when to close the window and continue with the method.

on the window if i use ShowDialog, the events aren’t caught (i’m assuming because the thread is tied up waiting on the dialog to close). But if i use Show, the method rolls on. i need it to wait. i’ve tried Thread.Sleep until some boolean value is switched on, but that ties up the thread too and makes the window unusable (using .Show()).

I’ve tried making a backgroundWorker, but i still run into the same problem.

i understand i could use codebehind on the buttons, but for the OK button, the viewModel has to verify that its state is valid. having the view consult with the viewmodel defeats the point of MVVM’s loose coupling style, right?

I’m learning WPF and MVVM all on my own, and i want to do it right and stick to good design principals where practical. how should i do this? what am i doing wrong? thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:20:53+00:00Added an answer on June 1, 2026 at 11:20 am

    You can correctly do this using Prism and a WindowRegionAdapter.

    1. Register the view(user control) that you want to show/hide with the WindowRegionAdapter so that when the view is requested it opens up in a new window.
    2. Create CompositePresentationEvents for Show/Hide/Close of the view and put them in some common infrastructure assembly.
    3. In the Initialize method of the the module containing the view, subscribe to the events created in 2 and and link them to OnShow/OnHide/OnClose event handlers.
    4. In the event handlers you can use the Prism region manager to add/remove the view from the region which will in turn show/hide/close the window. You can modify the WindowRegionAdapter to do precisely what you want it to do.
    5. Then whenever you want to show/hide the view you can simply publish the appropriate show/hide events wherever (in whichever module/viewmodel) you want and the appropriate event handler will be called in the module subscribing to the event and the window will be shown hidden.

    Yes its slightly complicated but absolutely worth the investment especially if you need open other windows as well. Keeps things very clean.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Window that has a ListBox ListBox(MyListBox) has a DataTable for its DataContext
I have a WPF Window with a view model set as its DataContext, and
I have a TreeView that launches a new window when each of its TreeViewItems
Hi I have a mainView window which has its dataContext set to it's own
I have a window named 'winAppt.xaml' and a view model called 'ItemViewModel.vb'. I would
I'm trying to hide window after its startup. I have own window-class which is
I have a webapp which resizes its window to exactly fit its contents: window.resizeTo(200,300)
I have a WPF application that contains some Window s with few UserControl s
I have a wpf application with a simple window that shows a text in
I have a comboBox that has a datatrigger that set its SelectedIndex based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.