2011-01-14 61 views
0

我想在WPF中繪製兩個形狀並將它們合併在一起。然後,我想將拖放事件附加到原始形狀之一。在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 

此代碼創建兩個圓圈(一個大的和小的)。我希望只能在點擊小圖標時才能拖動,但我想將它們連接起來,以便它們可以一起移動,而無需手動添加代碼來處理此問題。

回答

1

如果您將它們都放在Grid(或Canvas,StackPanel等)中,並在面板上設置拖動行爲,它們將被「合併」。如果在ellipse_2上將IsHitTestVisible設置爲false,它將不會響應任何鼠標事件,因此實際上它不可拖動。