我在WPF中有一個小項目,在這個項目中我需要交換UIElements。類似iGoogle的功能。爲什麼Rect.Intersect爲兩個不相交的矩形返回一個非空Rect?
由於我無法發佈圖片(沒有足夠的聲望),我會在文本中解釋。我有這樣定義一個3x3柵格:
0 1 2
0 C e C
1 e e e
2 L e C
其中C =帆布,L =標籤,E =空細胞(列+行)。
在MouseMove事件中,我跟蹤當前選定的畫布,並查看網格中可用的所有其他畫布的列表以檢查它們是否重疊。問題來了;即使我將畫布從(0,0)向右移動1個像素,它會檢測到它與來自(2,2)的畫布相交。
我正在使用Rect.Intersect(r1,r2)來確定相交區域,它應該返回一個空Rect,因爲r1不與r2重疊,而是總是返回一個非空Rect。
// Create the rectangle with the moving element width and height
Size draggedElementSize = new Size(this.DraggedElement.ActualWidth, this.DraggedElement.ActualHeight);
Rect draggedElementRect = new Rect(draggedElementSize);
foreach (Canvas c in canvases)
{
// Create a rectangle for each canvas
Size s = new Size(c.ActualWidth, c.ActualHeight);
Rect r = new Rect(s);
// Get the intersected area
Rect currentIntersection = Rect.Intersect(r, draggedElementRect);
if (currentIntersection == Rect.Empty) // this is never true
return;
} // end-foreach
我在做循環內的各種其他事情,但他們不以任何方式與此交互,因爲這不能正常工作。
我會很感激任何幫助。
謝謝。
也許你可以使用[拖放](http://msdn.microsoft.com/en-us/library/ms742859.aspx)功能實現?那麼你不必自己打擊測試。如果有東西被拖放到你的控制器上,你可以交換控件。 – nvoigt 2013-03-07 16:39:17
我不會將元素移動到我的畫布上。我將在網格上的不同位置移動畫布(包含多個元素)。但我會閱讀你給我的鏈接,也許它會幫助我,因爲我對WPF很陌生。 – Andrei 2013-03-07 16:51:48