In WPF, The actualheight of the control is sometimes used to perform related operations. I need to dynamically Add a button in an uniformgrid, and then obtain the actualheight of the button and the coordinates relative to the uniformgrid. The test project code is as follows (vs2010 ):
XAML section:
<Window X: class = "wpfapplication2.mainwindow" <br/> xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" <br/> xmlns: X = "http://schemas.microsoft.com/winfx/2006/xaml" <br/> Title = "mainwindow" Height = "450" width = "525"> <br/> <grid. columndefinitions> <br/> <columndefinition/> <br/> </grid. columndefinitions> <br/> <grid. rowdefinitions> <br/> <rowdefinition Height = "*"/> <br/> <rowdefinition Height = "5 *"/> <br/> </grid. rowdefinitions> <br/> <button name = "btntest" grid. column = "0" grid. row = "0" Height = "50" Click = "btntest_click"> testheight </button> <br/> <uniformgrid name = "UFG" Height = "300" grid. row = "1" margin = "0, 21"> <br/> </uniformgrid> <br/> </GRID> <br/> </WINDOW>
C # section:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. windows; <br/> using system. windows. controls; <br/> using system. windows. data; <br/> using system. windows. documents; <br/> using system. windows. input; <br/> using system. windows. media; <br/> using system. windows. media. imaging; <br/> using system. windows. navigation; <br/> using system. windows. shapes; <br/> namespace wpfapplication2 <br/>{< br/> /// <summary> <br/> // interaction logic for mainwindow. XAML <br/> /// </Summary> <br/> Public partial class mainwindow: Window <br/>{< br/> Public mainwindow () <br/>{< br/> initializecomponent (); <br/>}< br/> private void btntest_click (Object sender, routedeventargs E) <br/>{< br/> This. UFG. children. clear (); <br/> for (INT I = 0; I <20; I ++) <br/> {<br/> button BTN = new button (); <br/> BTN. content = I. tostring (); <br/> BTN. click + = new routedeventhandler (btn_click); <br/> This. UFG. children. add (BTN); <br/>}< br/> button B; <br/> generaltransform transform; <br/> point thepoint; <br/> B = UFG. children [10] As button; // The actual height of B is 60 <br/> MessageBox. show ("before updateuniformgrid children [10]. actualheight: {0} ", <br/> B. actualheight. tostring (); <br/> transform = B. transformtoancestor (UFG); <br/> thepoint = transform. transform (new point (0, 0); <br/> MessageBox. show ("before updateuniformgrid the point. Y relative to uniformgrid of children [10] (transformtoancestor): {0} ", <br/> thepoint. y. tostring (); <br/> MessageBox. show ("before updateuniformgrid the point. Y relative to uniformgrid of children [10] (translatepoint): {0} ", <br/> B. translatepoint (new point (0, 0), UFG ). y. tostring (); <br/> UFG. updatelayout (); // update the uniformgrid interface. It is important to obtain actualheight, but the relative coordinates are not available <br/> MessageBox. show ("after updateuniformgrid children [10]. actualheight: {0} ", <br/> B. actualheight. tostring (); <br/> transform = B. transformtoancestor (UFG); <br/> thepoint = transform. transform (new point (0, 0); <br/> MessageBox. show ("after updateuniformgrid, the point. Y relative to uniformgrid of children [10] (transformtoancestor): {0} ", <br/> thepoint. y. tostring (); <br/> MessageBox. show ("after updateuniformgrid, the point. Y relative to uniformgrid of children [10] (translatepoint): {0} ", <br/> B. translatepoint (new point (0, 0), UFG ). y. tostring (); <br/>}< br/> // click the event button in uniformgrid <br/> private void btn_click (Object sender, eventargs E) <br/>{< br/> MessageBox. show (sender as button ). actualheight. tostring (); <br/> button B = sender as button; <br/> MessageBox. show ("the point. Y relative to uniformgrid: {0} ", <br/> B. translatepoint (new point (0, 0), UFG ). y. tostring (); <br/>}</P> <p >}< br/>
After running the test project, you will find that actualheight is not obtained before updatalayout, but relative coordinates can be obtained. After updatalayout, actualheight and relative coordinates can be obtained. On the Internet, it is said that the WPF control can get actualheight only after render, and updatalayout should have this function. It is initially exposed to WPF, so the specific principle is not clear yet.
When obtaining relative coordinates, you can use transformtoancestor or translatepoint in the btntest_click event outside the for loop and in the uniformgrid button to click the event, in fact, there are other methods to get the relative coordinates of the control, which are not listed here. If you need them, You can Google them.
Attachment downloading (2010 Project)