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 8369123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:34:04+00:00 2026-06-09T13:34:04+00:00

I have a window that essentially runs a timer. When the timer hits 0

  • 0

I have a window that essentially runs a timer. When the timer hits 0 I want to bring the window to the front so that it is visible and not hidden behind some other application.

From what I can gather I would simply call window.activate() to accomplish this but with mvvm my view model doesn’t have a reference to window.

  • 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-09T13:34:05+00:00Added an answer on June 9, 2026 at 1:34 pm

    You could go about it in a couple of ways – adding a reference to the window could work since the viewmodel is not coupled with the view but related to it, but I don’t really like that approach since it pretty much does couple your view to your viewmodel – which is not really the point of MVVM

    A better approach may be to have your viewmodel raise an event or a command which the view can handle. This way the view gets to decide what UI action is associated with the command/event

    e.g. simply

    class SomeView 
    {
        void HandleSomeCommandOrEvent() 
        {
            this.Activate();
        }
    }
    

    Of course how you wire this up is up to you but I’d probably try and get routed commands happening

    Edit: You can’t really ‘bind’ a simple event, since it’s invoked from the viewmodel.

    A simple event based example is just to add the event to the viewmodel and handle it directly … e.g. imagine the following MainWindow with a ViewModel property

    public partial class MainWindow : Window
    {
        MainWindowViewModel ViewModel { get; set; }
    
        public MainWindow()
        {
            InitializeComponent();
    
            ViewModel = new MainWindowViewModel();
            ViewModel.ShowMessage += ViewModel_ShowMessage;
            this.DataContext = ViewModel;
        }
    
        void ViewModel_ShowMessage(object sender, ShowMessageEventArgs e)
        {
            MessageBox.Show(e.Message, "Some caption", MessageBoxButton.OK);
        }
    
    }
    

    Then the ViewModel can just fire the event:

    // The view model
    public class MainWindowViewModel
    {
        // The button click command
        public RelayCommand ButtonClickCommand { get; set; }
    
        // The event to fire
        public event EventHandler<ShowMessageEventArgs> ShowMessage;
    
        public MainWindowViewModel()
        {            
            ButtonClickCommand = new RelayCommand(ButtonClicked);            
        }
    
        void ButtonClicked(object param)
        {
            // This button is wired up in the view as normal and fires the event
            OnShowMessage("You clicked the button");
        }
    
        // Fire the event - it's up to the view to decide how to implement this event and show a message
        void OnShowMessage(string message)
        {
            if (ShowMessage != null) ShowMessage(this, new ShowMessageEventArgs(message));
        }
    }
    
    public class ShowMessageEventArgs : EventArgs
    {
        public string Message { get; private set; }
    
        public ShowMessageEventArgs(string message)
        {
            Message = message;
        }
    }
    

    The XAML would be:

    <Button Command="{Binding ButtonClickCommand}">Click me!</Button>
    

    So the button invokes the command, which in turn fires the event which the view (MainWindow) handles and shows a messagebox. This way the view/UI decides on the course of action based on the type of event raised. Of course it could be your timer which fired the event

    You can always go down the more involved route such as some of the answers on this question…

    How should the ViewModel close the form?

    but to be honest, it depends if you really need it – a simple event works well – some people overcomplicate things for the sake of elegance, but at the detriment of simplicity and productivity!

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

Sidebar

Related Questions

I have a ComboBox on a WPF window that is giving me some heartache,
I have a window that does not have a title bar ( WindowStyle ==
I have a window that uses a viewmodel. This screen contains 2 listviews on
I have a window that contains one QTMovieView. How do I make it so
I have a window that I sometimes open using Show() and sometimes using ShowDialog()
I have a window that is databound to a ViewModel. The ViewModel has many
I have a window that contains several rather complex views. Right now, I'm using
So I'm trying to have a window that I can drag around by the
I'm running into a problem where, I have a Window that contains a child
I have a modal window that uploads files to server. Works great. Upon completion

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.