1

ユーザーが画面上でいくつかのラベルをドラッグアンドドロップできるWinformsアプリがあります。

目的は、一致するラベルを互いに重ねることです。

私はこれらのラベルへの参照をリストに保持しており、現時点では、次のようにして、それらが重複しているかどうかを確認しています。

    foreach (List<Label> labels in LabelsList)
        {
            var border = labels[1].Bounds;
            border.Offset(pnl_content.Location);

            if (border.IntersectsWith(labels[0].Bounds))
            {
                labels[1].ForeColor = Color.Green;
            }
            else
            {
                labels[1].ForeColor = Color.Red;
            }
        }

問題は、これがWinforms(Bounds.Intersect)にのみ有効であるということです。同じ結果を達成するためにWPFで何ができますか?

それが違いを生む場合、私は現在<ItemsControl>、私の見解では両方のラベルを異なるものに追加しています。

4

1 に答える 1

4

コメントのおかげで、必要なことを行うことができました。

自宅で遊んでいるすべての人にとって、WPF コードは次のようになります。

    public void Compare()
    {

        foreach (List<Label> labels in LabelsList)
        {
            Rect position1 = new Rect();
            position1.Location = labels[1].PointToScreen(new Point(0, 0));                
            position1.Height = labels[1].ActualHeight;
            position1.Width = labels[1].ActualWidth;

            Rect position2 = new Rect();
            position2.Location = labels[0].PointToScreen(new Point(0, 0));
            position2.Height = labels[0].ActualHeight;
            position2.Width = labels[0].ActualWidth;

            if (position1.IntersectsWith(position2))
            {
                labels[1].Foreground = new SolidColorBrush(Colors.Green);
                continue;
            }

            labels[1].Foreground = new SolidColorBrush(Colors.Red);
        }
    }
于 2012-01-26T00:40:27.953 に答える