.net - Hit testing with new UIElements -
.net - Hit testing with new UIElements -
i creating wpf-usercontrol needs check new uielements not overlap existing uielements. code below works fine when baserectangle added canvas before phone call button1_click, if rectangle added in button1_click method hittest 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 hittest solves problem.
.net wpf hittest uielement
Comments
Post a Comment