c# - TransformToAncestor doesn't always work -


i'm working on first wpf app. i'm getting data xml file , based on file populate stackpanel dynamically created buttons.

later on need retrieve x coordinate specific button use method:

private double getstationcoordinates(int stationidx)     {         foreach (button station in stationmap.children)         {             if (stationidx != convert.toint16(station.tag)) continue;             var relativepoint = station.transformtoancestor(application.current.mainwindow).transform(new point(0, 0));             //console.writeline(stationidx + " " + relativepoint.tostring());             return relativepoint.x;         }         return 0;     } 

and use coordinates paint points in other panel under buttons. worked fine until set main window property

sizetocontent="widthandheight" 

now - when paint points first time after launching app (and after populate stackpanel buttons) (points) receive same x coordinate - 11. when hit refresh button ok.but first paint - after adding buttons problematic. same problem occurs when reload buttons configuration xml file. i'm starting clearing children in stackpanel , populating new ones. after first painting of points broken - getstationcoordinates returns 11 every button. somehow connected autosize property of main window?

you're trying position of elements haven't been correctly positioned yet. defer code until view has loaded, using of these 2 approaches:

a) subscribe loaded event of view , there.

private void window_loaded(object sender, eventargs e) {     this.loaded -= window_loaded;  // or else called again later      int id;     // stuff      getstationcoordinates(id); } 

b) put in asynchronous call dispatcher.begininvoke

private void somecode() {     int id;     // other stuff      dispatcher.begininvoke(new action(() =>         {             getstationcoordinates(id);         }),         dispatcherpriority.loaded); } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -