How to move borderless wpf window without code behind file
Asked Answered
S

5

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?

Supramolecular answered 2/10, 2013 at 5:50 Comment(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.Reparable
A
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!

Amey answered 2/10, 2013 at 12:29 Comment(0)
P
1

I think your best option is a behavior.

http://wpftutorial.net/Behaviors.html

Polymorphous answered 2/10, 2013 at 5:56 Comment(2)
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 :-)Supramolecular
@Supramolecular Even if the example in that article doesn't match your needs, a Behavior can still solve your problem.Polymorphous
W
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).

Wray answered 2/10, 2013 at 6:43 Comment(0)
L
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/

Larkin answered 2/10, 2013 at 6:12 Comment(1)
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.Supramolecular
G
-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!

Galileo answered 31/12, 2015 at 21:0 Comment(2)
This have code behind in the view, i was looking for something that will gave me the control of the view through vm.Determinate
What do you want to achieve by having this part entirely in mvvm?Vaivode

© 2022 - 2024 — McMap. All rights reserved.