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

时间:2013-10-02 05:50:37

标签: c# wpf xaml mvvm caliburn.micro

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

在几篇文章中我发现了以下解决方案:

XAML:

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

代码背后:

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

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

有什么想法吗?

5 个答案:

答案 0 :(得分:7)

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

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

查看此Codeproject文章了解详情!

答案 1 :(得分:1)

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

http://wpftutorial.net/Behaviors.html

答案 2 :(得分:1)

你可以下载Microsoft.Windows.Shell dll(Link。你可以找到谷歌的另一个下载选项),它给你一个CaptionHeight的属性,使tou能够从顶部拖动窗口(如正常的窗口)。

答案 3 :(得分:0)

答案 4 :(得分:0)

我知道我对这个问题有点迟了,但这就是我现在使用的一段时间,它就像一个魅力。

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

和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());
    }

希望这有帮助!