如何移动粘附/吸附WPF窗口

4
我希望当我移动“主”窗口时,可以同时移动两个或更多的固定窗口。
我希望能够实现以下操作:
private void MainWindow_PreviewMouseMove(object sender, MouseEventArgs e) {
  if (e.LeftButton == MouseButtonState.Pressed) {
    this.DragMove();
    foreach (var window in App.Current.Windows.OfType<Window>()) {
      window.Move(); // move it
    }
  }
}

我想使用这个解决方案来对窗口进行吸附操作。这是一个WPF的Snapping / Sticky / Magnetic Windows解决方案,详情请访问http://programminghacks.net/2009/10/19/download-snapping-sticky-magnetic-windows-for-wpf/。但我该如何移动它呢? 编辑 得到Gustavo Cavalcanti的回复后,我想了一些。这里有一个大概的解决方法来回答我的问题。
using System.Windows;
using System.Windows.Data;

namespace DragMoveForms
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1() {
      this.InitializeComponent();
    }

    public Window1(Window mainWindow)
      : this() {

      var b = new Binding("Left");
      b.Converter = new MoveLeftValueConverter();
      b.ConverterParameter = mainWindow;
      b.Mode = BindingMode.TwoWay;
      b.Source = mainWindow;

      BindingOperations.SetBinding(this, LeftProperty, b);

      b = new Binding("Top");
      b.Converter = new MoveTopValueConverter();
      b.ConverterParameter = mainWindow;
      b.Mode = BindingMode.TwoWay;
      b.Source = mainWindow;

      BindingOperations.SetBinding(this, TopProperty, b);
    }
  }
}

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace DragMoveForms
{
  public class MoveLeftValueConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
      // ok, this is simple, it only demonstrates what happens
      if (value is double && parameter is Window) {
        var left = (double)value;
        var window = (Window)parameter;
        // here i must check on which side the window sticks on
        return left + window.ActualWidth;
      }
      return 0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
      return DependencyProperty.UnsetValue;
    }
  }
}

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace DragMoveForms
{
  public class MoveTopValueConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
      // ok, this is simple, it only demonstrates what happens
      if (value is double && parameter is Window) {
        var top = (double)value;
        var window = (Window)parameter;
        // here i must check on which side the window sticks on
        return top;
      }
      return 0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
      return DependencyProperty.UnsetValue;
    }
  }
}
1个回答

5

在窗口的左侧和顶部使用数据绑定。使用转换器来根据主窗口确定正确的左/顶部位置。然后只需担心移动主窗口,其他窗口将相应移动。


你的解决方案有效,谢谢!那么主窗口的调整大小呢?在宽度改变时,我还能修改LeftProperty吗? - Li3ro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接