Tutorial :How should I move a WPF Window using MVVM?


This is probably overkill on the MVVM pattern but it's new to me and I'm interested to see if it is possible.

If I attach to the MouseMove event for a Window and do DragMove, I can move a bordeless window. Can I achieve this by some other method in MVVM or should I just accept adding this code to the Window's codebehind?


This is pure UI logic and doesn't belong in a ViewModel. The only reason you wouldn't want to put this in your code-behind would be for re-use and that is better solved with a custom Window derived control.


Personally I think any solution using MVVM would not make this code any better. Also, this is typically something that's view related and hasn't got anything to do with the data you're displaying.


IMHO, unless this is something that effects your data (aka the Model) then it is View code and should be in the View's code-behind and not in the Model.


I'm going to actually answer your question. The answer is yes. I'm using Cinch to assist me in the event binding and view model creation. The solution uses DragMove, but not as part of the code-behind (which is what I believe you are asking).

Event binding in the XAML:

<Window       xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"      xmlns:cinchV2="clr-namespace:Cinch;assembly=Cinch.WPF"      ...>      <i:Interaction.Triggers>          <i:EventTrigger EventName="MouseLeftButtonDown">              <cinchV2:EventToCommandTrigger Command="{Binding MouseLeftButtonDown}" />          </i:EventTrigger>      </i:Interaction.Triggers>      <Grid>          ...      </Grid>  </Window>  

In the ViewModel:

[ExportViewModel("MainViewModel")]  [PartCreationPolicy(CreationPolicy.NonShared)]  internal sealed class MainViewModel : ViewModelBase  {      public SimpleCommand<object, EventToCommandArgs> MouseLeftButtonDown { get; private set; }        [ImportingConstructor]      public MainViewModel(IUIVisualizerService uiVisualizerService)      {          ...          MouseLeftButtonDown = new SimpleCommand<object, EventToCommandArgs>(OnMouseLeftButtonDown);      }        private static void OnMouseLeftButtonDown(EventToCommandArgs e)      {          ((Window)e.Sender).DragMove();      }  }  

Fairly simple, right? Any events that come from the UI contain the View as the sender. So, here, we simply call the method on the view within the event handler in the ViewModel.

The project I'm working on uses no code-behind (even if it is not recommended in MVVM).


I know that I am a little late to the question, but this is what I have been using for sometime now and it works like a charm.

DashboardViewModel viewModel;  public DashboardView()  {      InitializeComponent();      viewModel = new DashboardViewModel();      viewModel.RequestClose += (s, e) => Application.Current.Dispatcher.Invoke(this.Close);      viewModel.RequestMinimize += (s, e) => Application.Current.Dispatcher.Invoke(() => { this.WindowState = WindowState.Minimized; });      DataContext = viewModel;  }  

and something like this in your viewModel

#region Public Event Handlers  public event EventHandler<EventArgs> RequestClose;  public event EventHandler<EventArgs> RequestMinimize;  

Next Post »