How to move borderless wpf window without code beh

2020-07-17 15:16发布

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?

5条回答
你好瞎i
2楼-- · 2020-07-17 15:37

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).

查看更多
祖国的老花朵
3楼-- · 2020-07-17 15:37

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!

查看更多
小情绪 Triste *
5楼-- · 2020-07-17 15:58

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!

查看更多
beautiful°
6楼-- · 2020-07-17 15:59

I think your best option is a behavior.

http://wpftutorial.net/Behaviors.html

查看更多
登录 后发表回答