如何在没有代码隐藏文件的情况下移动无边框 wpf 窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19130722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:46:09  来源:igfitidea点击:

How to move borderless wpf window without code behind file

c#wpfxamlmvvmcaliburn.micro

提问by rhe1980

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

我正在创建一个带有无边框窗口的 WPF 应用程序。应用MVVVM模式(在 Caliburn.Micro 的帮助下)我没有代码隐藏文件,只有一个 XAML 文件。

In several posts I found following solution:

在几篇文章中,我找到了以下解决方案:

XAML:

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.

现在我正在寻找一个解决方案来在 XAML 中完全定义它。

Any idea?

任何的想法?

回答by Ibrahim Najjar

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

我将提出的解决方案并不是真正建议的,但您可以将代码放在您的 XAML 文件中,如下所示:

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

Check this Codeprojectarticle for more information on this!

查看此Codeproject文章以获取更多信息!

回答by Foole

I think your best option is a behavior.

我认为你最好的选择是一种行为。

http://wpftutorial.net/Behaviors.html

http://wpftutorial.net/Behaviors.html

回答by Programmer

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

您可以下载 Microsoft.Windows.Shell dll(链接。您可以使用 google 找到另一个下载选项),它为您提供 CaptionHeight 属性,使您可以从顶部拖动窗口(就像普通窗口一样)。

回答by Edd

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

在你的 viewModel 中类似这样的东西

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

Using the ICommand interface...

使用 ICommand 接口...

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

这是 RelayCommand 类。

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!

希望这可以帮助!