c# - How to remove/delete ContentControl in Canvas WPF -
i'm starting wpf , have problem. please me. thanks, sorry bad english!
i have added contentcontrol canvas, , want remove/delete it.
draw contentcontrol code:
contentcontrol cc = new contentcontrol(); cc.content = shape; cc.height = h; cc.width = w; style s = mycanvas.findresource("designeritemstyle") style; cc.style = s; canvas.setleft(cc, x); canvas.settop(cc, y); mycanvas.children.add(cc);
i use hittest remove can remove shape
private void mycanvas_mouserightbuttondown(object sender, mousebuttoneventargs e) { point pt = e.getposition((canvas)sender); hittestresult result = visualtreehelper.hittest(mycanvas, pt); if (result != null) { mycanvas.children.remove(result.visualhit shape); //it works shape // have changed mycanvas.children.remove(result.visualhit contentcontrol); //but didn't work contentcontrol } }
it because contentcontrol
parent of shape
, , canvas
's children contains contentcontrol
hosts shape. fix issue :)
private void mycanvas_mouserightbuttondown(object sender, mousebuttoneventargs e) { dependencyobject ob = findancestor<contentcontrol>((dependencyobject)e.source); if (ob.gettype() == typeof(contentcontrol)) mycanvas.children.remove(ob contentcontrol); } public t findancestor<t>(dependencyobject dependencyobject) t : dependencyobject { var parent = visualtreehelper.getparent(dependencyobject); if (parent == null) return null; var parentt = parent t; return parentt ?? findancestor<t>(parent); }
Comments
Post a Comment