我需要測量按鈕(和單選按鈕)的DesiredSize或ActualHeight/Width,但實際上並沒有將它放到可視化樹上,但我總是找回無意義的值。測量其他控件(如TextBlock)時,這種方法也適用。UWP XAML中的按鈕和單選按鈕的DesiredSize
var button = new Button
{
Content = "Hello World",
FontSize = 15
};
button.Measure(new Size(maxWidth, double.PositiveInfinity));
var height = button.DesiredSize.Height;
var width = button.DesiredSize.Width
我回來了21px的高度和0px的寬度。任何想法,爲什麼我回到寬度爲0?
謝謝埃爾維斯!這樣可行! –