5

I'm creating a WPF application with a borderless window. Applying the MVVVM pattern (with help of Caliburn.Micro) I do not have a code behind file but only a XAML file.

In several posts I found following solution:

XAML:

<Window
   ...
   WindowStyle="None" MouseLeftButtonDown="WindowMouseLeftButtonDown"/>

Code behind:

 private void WindowMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DragMove();
    }

Now I'm searching a solution to define this completely in XAML.

Any idea?

Thilina H
  • 5,754
  • 6
  • 26
  • 56
rhe1980
  • 1,557
  • 1
  • 15
  • 36
  • 1
    You could just create a subclass of Window that uses this approach and re-use it instead of `Window` whenever you need a movable borderless window. – Joey Oct 02 '13 at 08:19

5 Answers5

8

The solution I will present is not really advised, but you can put your code behind right in your XAML file like this:

<Window
...
WindowStyle="None" MouseLeftButtonDown="WindowMouseLeftButtonDown"/>
<x:Code>
    <![CDATA[            
        private void WindowMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }
    ]]>
</x:Code>

Check this Codeproject article for more information on this!

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
1

I think your best option is a behavior.

http://wpftutorial.net/Behaviors.html

Foole
  • 4,754
  • 1
  • 26
  • 23
  • thanks for the link. In my opinion this works only for controls inside a window, but not for the window itself. But nice to know for other cases :-) – rhe1980 Oct 03 '13 at 06:11
  • @rhe1980 Even if the example in that article doesn't match your needs, a Behavior can still solve your problem. – Foole Oct 03 '13 at 07:40
1

You can download the Microsoft.Windows.Shell dll (Link. You can find another download options with google), which gives you a property of CaptionHeight that enables tou to drag the window from its top part (like a normal window).

Programmer
  • 1,973
  • 1
  • 12
  • 20
0

You can use an EventCommandTrigger. Check these references:

http://www.danharman.net/2011/08/05/binding-wpf-events-to-mvvm-viewmodel-commands/ http://zamjad.wordpress.com/2011/06/07/convert-event-into-command-in-mvvm-model/

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • thanks for your answer. What I was searching for was a solution in XAML only, without using the viewmodel or the codebehind file from the view. – rhe1980 Oct 03 '13 at 06:13
-1

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;
    #endregion

Using the ICommand interface...

    #region ICommand Members
    public ICommand CloseCommand { get; private set; }
    public ICommand MinimizeCommand { get; private set; }
    #endregion

Configure the commands...

    private void SetupCommands()
    {
        CloseCommand = new RelayCommand(CloseApplication);
        MinimizeCommand = new RelayCommand(MinimizeApplication);
    }

Here is the RelayCommand class.

public class RelayCommand : ICommand
{
    #region Private Readonly Properties
    private readonly Action<object> executeCommand;
    private readonly Predicate<object> canExecute;
    #endregion

    #region Constructors
    public RelayCommand(Action<object> execute) : this(execute, null)
    {

    }
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null) 
            throw new ArgumentNullException("execute");
        this.executeCommand = execute; 
        this.canExecute = canExecute;
    }
    #endregion

    #region Public ICommand Members
    public bool CanExecute(object parameter)
    {
        return canExecute == null ? true : canExecute(parameter);
    }
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
    public void Execute(object parameter)
    {
        executeCommand(parameter);
    }
    #endregion
}

And some example methods...

    private void MinimizeApplication(object obj)
    {
        RequestMinimize(this, new EventArgs());
    }
    private void CloseApplication(object obj)
    {
        RequestClose(this, new EventArgs());
    }

Hope this helps!

Edd
  • 703
  • 7
  • 23
  • This have code behind in the view, i was looking for something that will gave me the control of the view through vm. – exSnake Oct 21 '16 at 23:55
  • What do you want to achieve by having this part entirely in mvvm? –  Feb 01 '18 at 14:35