You need to force a render of the item, or wait for the item to be rendered. You can then use the ActualHeight
and ActualWidth
properties.
To force a render:
MenuItem item = new MenuItem();
item.Header = "bling";
item.Icon = someIcon;
//Force render
item.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
item.Arrange(new Rect(item.DesiredSize));
In this example the MenuItem
has not been given an explicit height or width. However, forcing the render will render it taking the supplied header text and icon into consideration.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…