C# 在画布内拖动控件,以便在其中一个控件移动时,将另一个控件向同一方向移动

C# 在画布内拖动控件,以便在其中一个控件移动时,将另一个控件向同一方向移动,c#,wpf,canvas,drag-and-drop,C#,Wpf,Canvas,Drag And Drop,我有两个拇指,看起来像画布上的矩形。我有一个要求,如果一个矩形移动,我想移动另一个矩形(在同一方向上),即使用相同的e.水平变化和e.垂直变化,但它们不应移出画布的边界。。请帮忙 Xaml: 代码隐藏: 1) 动态填充Thumb控件 private void CreateUIShapes(int numberOfWindows, List<Dimensions> dimensions) { Thumb th = null;

我有两个拇指,看起来像画布上的矩形。我有一个要求,如果一个矩形移动,我想移动另一个矩形(在同一方向上),即使用相同的e.水平变化和e.垂直变化,但它们不应移出画布的边界。。请帮忙

Xaml:


代码隐藏:

1) 动态填充Thumb控件

private void CreateUIShapes(int numberOfWindows, List<Dimensions> dimensions)
        {
            Thumb th = null;
            for(int i = 0; i < numberOfWindows; i++)
            {
                th = new Thumb();
                th.Name = string.Format("{0}{1}", "Thumb", i + 1);
                var item = dimensions[i];
                th.Width = item.Width;
                th.Height = item.Height;
                th.Foreground = new SolidColorBrush(Windows.UI.Colors.Transparent);
                th.BorderBrush = item.BorderColor;
                th.BorderThickness = new Thickness(3);
                th.DragDelta += Th_DragDelta;
                th.DragCompleted += Th_DragCompleted;
                Canvas.SetLeft(th,item.left);
                Canvas.SetTop(th, item.Top);
                cnvBarCodeImage.Children.Add(th);
            }
        }
private void CreateUIShapes(int numberOfWindows,列表维度)
{
Thumb th=null;
对于(int i=0;i
2) 处理所选Thumb的DragDelta事件

   private void Th_DragDelta(object sender, DragDeltaEventArgs e)
        {
            var SelectedDraggableThumb = sender as Thumb;
            double left = (Canvas.GetLeft(SelectedDraggableThumb) > 0) ? Canvas.GetLeft(SelectedDraggableThumb) : 0; //Prevent moving left beyond the boundry
            double top = (Canvas.GetTop(SelectedDraggableThumb) > 0) ? Canvas.GetTop(SelectedDraggableThumb) : 0; //Prevent moving top beyond the boundry
            left = (Canvas.GetLeft(SelectedDraggableThumb) > BarCodeImage.ActualWidth - SelectedDraggableThumb.ActualWidth) ? BarCodeImage.ActualWidth - SelectedDraggableThumb.ActualWidth : left; //Prevent moving right beyond the boundry
            top = (Canvas.GetTop(SelectedDraggableThumb) > BarCodeImage.ActualHeight - SelectedDraggableThumb.ActualHeight) ? BarCodeImage.ActualHeight - SelectedDraggableThumb.ActualHeight : top;//Prevent moving bottom beyond the boundry
            Canvas.SetLeft(SelectedDraggableThumb, left + e.HorizontalChange);
            Canvas.SetTop(SelectedDraggableThumb, top + e.VerticalChange);

            //Set the dimensions for the other Thumb.
            var OtherDraggableThumb = cnvBarCodeImage.Children.OfType<Thumb>().Single(child => child.Name != SelectedDraggableThumb.Name); //I tried setting the e.HorizontalChange and e.VerticalChange like above but it dosen't work. How should I proceed from here. Please help.

        }
private void Th_DragDelta(对象发送方,DragDeltaEventArgs e)
{
var SelectedDraggableThumb=发送方作为拇指;
double left=(Canvas.GetLeft(SelectedDraggableThumb)>0)?Canvas.GetLeft(SelectedDraggableThumb):0;//防止向左移动超出边界
double-top=(Canvas.GetTop(SelectedDraggableThumb)>0)?Canvas.GetTop(SelectedDraggableThumb):0;//防止将顶部移动到边界之外
left=(Canvas.GetLeft(SelectedDraggableThumb)>BarCodeImage.ActualWidth-SelectedDraggableThumb.ActualWidth)?BarCodeImage.ActualWidth-SelectedDraggableThumb.ActualWidth:left;//防止向右移动超出边界
top=(Canvas.GetTop(SelectedDraggableThumb)>BarCodeImage.ActualHeight-SelectedDraggableThumb.ActualHeight)?BarCodeImage.ActualHeight-SelectedDraggableThumb.ActualHeight:top;//防止底部移动到边界之外
Canvas.SetLeft(选择dRaggablethumb,left+e.HorizontalChange);
Canvas.SetTop(选择dRaggablethumb,top+e.VerticalChange);
//设置另一个拇指的尺寸。
var OtherDraggableThumb=cnvBarCodeImage.Children.OfType().Single(child=>child.Name!=SelectedDraggableThumb.Name);//我尝试过像上面那样设置e.HorizontalChange和e.VerticalChange,但都不起作用。我应该如何从这里开始。请帮助。
}
3) 传递数据以创建拇指

var dimensions = new List<Dimensions>();
dimensions.Add(new Dimensions() { Height = 200, Width = 400, left = 100, Top = 60, BorderColor = new SolidColorBrush(Windows.UI.Colors.Green) });
dimensions.Add(new Dimensions() { Height = 200, Width = 400, left = 100, Top = 280, BorderColor = new SolidColorBrush(Windows.UI.Colors.Blue) });
CreateUIShapes(2, dimensions);
var维度=新列表();
添加(新维度(){Height=200,Width=400,left=100,Top=60,BorderColor=newsolidcolorbush(Windows.UI.Colors.Green)});
添加(新维度(){Height=200,Width=400,left=100,Top=280,BorderColor=newsolidColorBrush(Windows.UI.Colors.Blue)});
CreateUI形状(2,尺寸);

您的具体问题是什么?沟通三角洲?我会考虑使用MVVMLMessenger,这样可以解耦。这是酒馆子模式。任何拇指的移动都会发出一条信息,表示它有一个x&y的dragdelta。任何其他订阅该消息类型的用户。当收到一条消息时,它会检查它不是发送给它的消息,并应用更改。感谢您的建议。你可以发布一些示例代码,这样我就可以开始了。
var dimensions = new List<Dimensions>();
dimensions.Add(new Dimensions() { Height = 200, Width = 400, left = 100, Top = 60, BorderColor = new SolidColorBrush(Windows.UI.Colors.Green) });
dimensions.Add(new Dimensions() { Height = 200, Width = 400, left = 100, Top = 280, BorderColor = new SolidColorBrush(Windows.UI.Colors.Blue) });
CreateUIShapes(2, dimensions);