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 的吸附/粘性/磁性窗口 http://programminghacks.net/2009/10/19/download-snapping-sticky-magnetic-windows-for-wpf/

但我怎样才能移动它?

编辑

在得到古斯塔沃·卡瓦尔康蒂的回复后,我产生了一些想法。这是我的问题的粗略解决方案。

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

1 回答 1

5

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

于 2011-11-17T22:33:50.850 回答