WPF Shape bounding rectangle is always empty -


i have following code in wpf application:

public mainwindow()     {         initializecomponent();         p1 = new point(50, 50);         p2 = new point(355, 50);         p3 = new point(50, 355);         p4 = new point(355, 355);          loaded += (x, y) => draw();         //contentrendered += (x, y) => draw();     } 

i create polygon shape , try bounding rectangle several ways:

 private void draw()     {         polygon polygon = new polygon();         polygon.stroke = system.windows.media.brushes.white;         polygon.points.add(p1);         polygon.points.add(p2);         polygon.points.add(p3);         polygon.points.add(p4);         canvas.children.add(polygon);         boundingrect = polygon.transformtovisual(canvas).transformbounds(new rect(polygon.rendersize));         boundingrect = polygon.transformtovisual(polygon).transformbounds(system.windows.controls.primitives.layoutinformation.getlayoutslot(polygon));         boundingrect = polygon.rendertransform.transformbounds(new rect(polygon.rendersize));         boundingrect = getrectofobject(polygon);         boundingrect = polygon.renderedgeometry.bounds;     }   private rect getrectofobject(frameworkelement _element)     {         rect rectanglebounds = new rect();         rectanglebounds = _element.rendertransform.transformbounds(new rect(0, 0, _element.actualwidth, _element.actualheight));         return rectanglebounds;     } 

however, {0;0;0;0}.

on debug, can see points size rendered 0:

enter image description here

how should calculate correct way?

according msdn remarks of measure method, calling updatelayout obtain necessary data (though should treated carefully), way measuring works:

private void draw() {     polygon polygon = new polygon();     polygon.stroke = system.windows.media.brushes.white;     polygon.points.add(p1);     polygon.points.add(p2);     polygon.points.add(p3);     polygon.points.add(p4);     canvas.children.add(polygon);     polygon.updatelayout();     boundingrect = polygon.transformtovisual(canvas).transformbounds(new rect(polygon.rendersize));        } 

thanks @chrisf hints.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -