我有一个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>中。
发布于 2012-01-26 08:40:28
因此,多亏了这些评论,我才能做我需要做的事情。
对于所有在家玩的人来说,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);
}
}https://stackoverflow.com/questions/9003201
复制相似问题