4

「メイン」ウィンドウを移動するときに、2つ以上のスティッキーウィンドウを移動したい

このようなことをしたい

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 に答える