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");
}
}
Calling canvas1.UpdateLayout() before the hittest solves the problem.