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

本文关键字:移动 边框 wpf 文件 窗口 情况下 代码 | 更新日期: 2023-09-27 18:33:29

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

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

XAML:

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

代码隐藏:

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

现在,我正在寻找一种解决方案,以在 XAML 中完全定义它。

知道吗?

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

我将介绍的解决方案并不是真正建议的,但你可以像这样将代码放在 XAML 文件中:

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

查看此代码项目文章以获取有关此内容的更多信息!

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

http://wpftutorial.net/Behaviors.html

您可以下载 Microsoft.Windows.Shell dll (链接。您可以使用Google找到其他下载选项),它为您提供了CaptionHeight的属性,该属性使tou能够将窗口从其顶部(如普通窗口)拖动。

您可以使用EventCommandTrigger .查看以下参考资料:

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/

我知道

我回答这个问题有点晚了,但这是我一段时间以来一直在使用的,它就像一个魅力。

    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;
    }

和类似的东西在你的视图模型

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

使用ICommand接口...

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

配置命令...

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

下面是 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
}

还有一些示例方法...

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

希望这有帮助!