Jargo
Jargo

Reputation: 365

Hit testing with new UIElements

I am creating a WPF-usercontrol that needs to check that new UIElements does not overlap with any existing UIElements. The code below works fine when the baseRectangle is added to canvas before the call to button1_Click, but if the rectangle is added in the button1_Click method the hittest does not work.

<Window x:Class="WpfCollisionTest.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>
        <Canvas Height="246" HorizontalAlignment="Left" Margin="12,12,0,0" Name="canvas1" VerticalAlignment="Top" Width="479"></Canvas>
        <Button Content="Button" Height="35" HorizontalAlignment="Left" Margin="12,264,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>


 public MainWindow()
 { 
     InitializeComponent();
 }

 private void button1_Click(object sender, RoutedEventArgs e)
 {
     canvas1.Children.Clear();
     Rectangle rect = new Rectangle();
     rect.Width = 200;
     rect.Height = 200;
     rect.Fill = Brushes.Red;
     canvas1.Children.Add(rect);
     if (VisualTreeHelper.HitTest(canvas1, new Point(100, 100)) != null)
     {
         MessageBox.Show("Collision");
     }
 }

Upvotes: 0

Views: 688

Answers (2)

Jargo
Jargo

Reputation: 365

Calling canvas1.UpdateLayout() before the hittest solves the problem.

Upvotes: 1

Paul Hoenecke
Paul Hoenecke

Reputation: 5060

Try adding this after adding the rect to the canvas:

canvas1.Children.Add(rect);
rect.LayoutUpdated += (s, args) =>
{
    if (VisualTreeHelper.HitTest(canvas1, new Point(100, 100)) != null)
    {
        MessageBox.Show("Collision");
    }
};

Of course, that would be hit each time the layout of rect is updated... so you may want to remove the handler, or do some further check inside it.

Upvotes: 0

Related Questions