【问题标题】:Merging Shapes and Attaching Partial Drag/Drop in WPF在 WPF 中合并形状并附加部分拖放
【发布时间】:2011-01-14 17:03:58
【问题描述】:

我想在 WPF 中绘制两个形状并将它们合并在一起。然后,我想将拖放事件附加到原始形状之一。

所以基本上,只有点击形状的某个部分才能拖动,但它会拖动整个形状。

这是一些代码:

// Set up some basic properties for the two ellipses
Point centerPoint = new Point(100, 100);
SolidColorBrush ellipseColor_1 = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
double width_1 = 10; double height_1 = 10;
SolidColorBrush ellipseColor_2 = new SolidColorBrush(Color.FromArgb(50, 255, 0, 0));
double width_2 = 200; double height_2 = 200;

// Create the first ellipse: A small blue dot
// Then position it in the correct location (centerPoint)
Ellipse ellipse_1 = new Ellipse() { Fill = ellipseColor_1, Width = width_1, Height = height_1 };
ellipse_1.RenderTransform = new TranslateTransform(point.X - width_1 / 2, point.Y - height_1 / 2);

// Create the second ellipse: A large red, semi-transparent circle
// Then position it in the correct location (centerPoint)
Ellipse ellipse_2 = new Ellipse() { Fill = ellipseColor_2, Width = width_2, Height = height_2 };
ellipse_2.RenderTransform = new TranslateTransform(point.X - width_2 / 2, point.Y - height_2 / 2);

// ???
// How should I merge these?
// ???

// Now apply drag drop behavior to ONLY ellipse_1
MouseDragElementBehavior dragBehavior = new MouseDragElementBehavior();
dragBehavior.Attach(ellipse_1);  // This may change depending on the above

// ...
// Add new element to canvas

此代码创建两个圆圈(一个大圆圈和一个小圆圈)。我希望只有在单击小的情况下才能拖动,但我希望将它们连接起来,这样它们就可以一起移动,而无需手动添加可以解决此问题的代码。

【问题讨论】:

    标签: c# wpf drawing shapes


    【解决方案1】:

    如果将它们都放在Grid(或CanvasStackPanel 等)中,并在面板上设置拖动行为,它们将被“合并”。如果您在 ellipse_2 上将 IsHitTestVisible 设置为 false,它不会响应任何鼠标事件,因此实际上它是不可拖动的。

    【讨论】:

      猜你喜欢
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多