2011-11-17 37 views
4

我想移動兩個或兩個以上粘窗戶當我移動「主」窗口我如何可以移動粘/搶購WPF窗口

我想要做這樣的事

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

現在看起來好多了! – SvenG

回答

5

在窗口的左側和頂部使用數據綁定。使用轉換器根據主窗口確定右側/頂部。然後只要擔心移動主窗口,其他人會相應地移動。

+0

感謝您的檢查,看看我編輯的問題 – punker76

+0

您的解決方案的作品,所以thanx!主窗口調整大小怎麼樣?在寬度變化,我仍然可以修改LeftProperty? – Li3ro