1

I have a Winforms app that allows the user to drag and drop some labels around the screen.

The objective being to put the matching labels on top of each other.

I keep a reference to these labels in a list, and at the moment i'm checking to see if they're overlapping by doing the following.

    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;
            }
        }

The problem being that this is only good for Winforms (Bounds.Intersect). What can I do in WPF to achieve the same result?

If it makes a difference, i'm currently adding both labels to different <ItemsControl> in my view.

Lucas
  • 643
  • 1
  • 9
  • 21
  • 3
    Have a look at: http://stackoverflow.com/questions/1554116/how-can-i-check-if-2-controls-overlap-eachother-on-a-canvas-in-wpf does this address your question? – Emond Jan 25 '12 at 13:33
  • Thanks Guys, you put me on the right track! – Lucas Jan 26 '12 at 00:36

1 Answers1

4

So thanks to the comments I was able to do what I needed.

The WPF code now looks like this for all those playing at home:

    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);
        }
    }
Lucas
  • 643
  • 1
  • 9
  • 21
  • 2
    Just remember that when you use RenderTransform or LayoutTransform on the labels your function will give false positives and false negatives – Emond Jan 26 '12 at 05:17