4

UIElements を交換する必要がある WPF の小さなプロジェクトがあります。iGoogle の機能に似たもの。

写真が載せられない(評判が悪い)ので、文章で説明します。次のように定義された 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) を使用して交差領域を決定していますが、r1 は r2 と重複していないため、空の Rect を返す必要がありますが、代わりに常に空でない 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

私はループ内で他のさまざまなことを行っていますが、これが適切に機能していないため、これとはまったく相互作用しません。

何かお役に立てば幸いです。

ありがとう。

4

2 に答える 2

1

コード内の位置への参照は見当たりません。幅と高さだけです。すべての長方形を 0/0 から開始しますか? ほとんどの場合、それらはすべて重複します。x/y 座標を含める必要があります。

于 2013-03-07T16:31:57.607 に答える
1

コード例のどこにも、四角形を場所でオフセットしていません。四角形のサイズのみを設定しています。

もちろん、すべての四角形は Point(0,0) から始まるため、すべて交差します。

rects をチェック対象の要素からその親に変換する必要があります。

これを実現する最も簡単な方法は、VisualTreeHelper.GetOffsetです。

    // 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);
    draggedElementRect.offset(VisualTreeHelper.GetOffset(this.DraggedElement));

    foreach (Canvas c in canvases)
    {
        if (this.DraggedElement == c) continue; // skip dragged element.
        // Create a rectangle for each canvas
        Size s = new Size(c.ActualWidth, c.ActualHeight);
        Rect r = new Rect(s);
        r.offset(VisualTreeHelper.GetOffset(c));

        // Get the intersected area
        Rect currentIntersection = Rect.Intersect(r, draggedElementRect);

        if (currentIntersection == Rect.Empty) // this is never true
            return;

    } // end-foreach

示されているように、現在ドラッグされている要素を確実にスキップしたい場合があります。

于 2013-03-07T16:32:01.123 に答える