0

I have a wpf application that needs to pop up in front all other windows based on certain conditions. I am trying to accomplish this in an MVVM compliant manner. After a lot of research, the best solution involves using the window's activate event and interface's behavior. I was able to get the activate function to work using the second solution found on mvvm activate . However, once I style my GUI using Mahapp Metro control "MetroWindow" based on the quickstart guide, the GUI messes up. Whenever the XAML interaction behavior section is added, the GUI's original Window object appears, resulting with 2 title bars (one from Window and other from MetroWindow). I tried designing my own window using Metro stylings but so far unable to implement the Metro title bar (can't find any guides or documentation on doing so). Visual studio also keeps complaining "Object reference not set to an instance of an object" (sometimes on

<controls:MetroWindow
...

and other times on

<i:Interaction.Behaviors>
    <Behaviors:ActivateBehavior ...

yet the application runs.

How do I keep the Mahapps Metro styles (including title bar style) and cause the window to appear in front as determined by my business logic in an MVVM compliant manner?

Community
  • 1
  • 1
SILENT
  • 3,916
  • 3
  • 38
  • 57

1 Answers1

0

It's not the answer to the essence of your problem, but maybe you can use something like this to activate a window:

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;    
using GalaSoft.MvvmLight.Messaging;

// View

public partial class TestActivateWindow : Window
{
    public TestActivateWindow() {
        InitializeComponent();
        Messenger.Default.Register<ActivateWindowMsg>(this, (msg) => Activate());
    }
}

// View Model

public class ActivateWindowMsg
{
}

public class MainViewModel: ViewModelBase
{
    ICommand _activateChildWindowCommand;

    public ICommand ActivateChildWindowCommand {
        get {
            return _activateChildWindowCommand?? (_activateChildWindowCommand = new RelayCommand(() => {
                Messenger.Default.Send(new ActivateWindowMsg());
        }));
        }
    }
}
romanoza
  • 4,775
  • 3
  • 27
  • 44
  • Isn't this non-mvvm compliant? – SILENT Aug 08 '14 at 21:11
  • @SILENT The ViewModel doesn't know anything about the View, so I don't think it's against the MVVM pattern. In fact, `TestActivateWindow` and `MainViewmodel` can be placed in different dll-s and can communicate each other thru binding and messages. – romanoza Aug 08 '14 at 21:21
  • Isn't ActivateWindowMsg part of the View and VM has to directly reference it? MVVM is getting even more confusing to me. – SILENT Aug 08 '14 at 23:51
  • @SILENT No, `ActivateWindowMsg` class is a part of VM. I have clarified the code. – romanoza Aug 09 '14 at 05:23