WPF如何检查画布中是否为空格

时间:2011-07-24 14:48:44

标签: c# wpf xaml

我使用Random在Canvas上添加矩形,因为我不知道宽度和高度。

代码

  HProDataContext db = new HProDataContext();

        var RoomX = (from d in db.rooms select d.sizex).ToList();
        var RoomY = (from d in db.rooms select d.sizey).ToList();
        var random = new Random();
        for (int i = 0; i < RoomX.Count; i++)
        {

            RoomX[i] = (Convert.ToDouble(RoomX[i]) * 10).ToString();
            RoomY[i] = (Convert.ToDouble(RoomY[i]) * 10).ToString();

            var rectangle = new Rectangle()
            {
                Stroke = Brushes.Black,
                Fill = Brushes.SkyBlue,
                Width = Convert.ToDouble(RoomX[i]),
                Height = Convert.ToDouble(RoomY[i]),
                Margin = new Thickness(
                    left: random.NextDouble() * 300,
                    top: random.NextDouble() * 150,
                    right: 0,
                    bottom: 0),
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };
            mainCanvas.Children.Add(rectangle);
        }
    }

效果很好,但有些矩形正在绘制另一个矩形 如何在空白处绘制矩形?

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以为每个WPF矩形计算一个Rect,并使用在Rect中找到的IntersectsWith方法来测试代码是否可以绘制一个没有重叠的新Rectangle。我还建议使用矩形属性Canvas.Left和Canvas.Top,而不是调整边距。

我已经包含了一个简单的示例,您可以测试它以帮助您入门。

这是一些包含4个矩形的XAML:

<Window x:Class="OverlapRectangeTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="60"/>
    </Grid.RowDefinitions>
    <Canvas x:Name="c" Grid.Row="0">
        <Rectangle x:Name="b1" Fill="Aqua" Canvas.Left="0" Canvas.Top="0" Width="100" Height="200"/>
        <Rectangle x:Name="b2" Fill="Bisque" Canvas.Left="0" Canvas.Top="0" Width="200" Height="100"/>
        <Rectangle x:Name="b3" Fill="BlueViolet"  Canvas.Left="250" Canvas.Top="0" Width="100" Height="200"/>
        <Rectangle x:Name="b4" Fill="Cornsilk"  Canvas.Left="111" Canvas.Top="0" Width="100" Height="200"/>
    </Canvas>
    <Button Grid.Row="1" Click="Button_Click"/>
  </Grid>
</Window>

这是一个按钮单击事件处理程序,我在其中测试IntersectsWith方法:

private void Button_Click(object sender, RoutedEventArgs e)
{
  double top1 = (double)b1.GetValue(Canvas.TopProperty);
  double left1 = (double)b1.GetValue(Canvas.LeftProperty);
  Rect rect1 = new Rect(left1, top1, b1.Width, b1.Height);

  double top2 = (double)b2.GetValue(Canvas.TopProperty);
  double left2 = (double)b2.GetValue(Canvas.LeftProperty);
  Rect rect2 = new Rect(left2, top2, b2.Width, b2.Height);

  double top3 = (double)b3.GetValue(Canvas.TopProperty);
  double left3 = (double)b3.GetValue(Canvas.LeftProperty);
  Rect rect3 = new Rect(left3, top3, b3.Width, b3.Height);

  double top4 = (double)b4.GetValue(Canvas.TopProperty);
  double left4 = (double)b4.GetValue(Canvas.LeftProperty);
  Rect rect4 = new Rect(left4, top4, b4.Width, b4.Height);

  bool rc0 = rect1.IntersectsWith(rect4);
  bool rc1 = rect1.IntersectsWith(rect2);
  bool rc2 = rect2.IntersectsWith(rect3);
  bool rc3 = rect3.IntersectsWith(rect1);
  bool rc4 = rect2.IntersectsWith(rect4);
}