Search code examples
c#.netwpfwindowssticky

how can i move sticky / snapping wpf windows


i want move two or more sticky windows when i move a "main" window

i want do something like this

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

i want use this solution for snapping the windows

Snapping / Sticky / Magnetic Windows for WPF http://programminghacks.net/2009/10/19/download-snapping-sticky-magnetic-windows-for-wpf/

but how can i move it?

EDIT

After the reply from Gustavo Cavalcanti, i made ​​a few thoughts. Here is a rough solution to my question.

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

Solution

  • Use data binding on the Left and Top of the windows. Use converters to determine the right left/top based on the main window. Then just worry about moving the main window, that the others will move accordingly.