怎么说代码背后的XAML <Button Height ="Auto"/>?

Edw*_*uay 35 c# wpf xaml autosize

你如何设置Height="*"Height="Auto"代码背后?

Nol*_*rin 59

要设置Height = "Auto"大多数控件,您需要使用赋值double.NaN.

例:

element.Height = double.NaN;
Run Code Online (Sandbox Code Playgroud)

设置Width/Height = "*"(是一个稍微不同的问题,因为它只适用于有选择的几个元件(ColumnDefinitionRowDefinition例如).该类型的Width/ HeightGridLength,而不是double.

示例(此MSDN页面上提供了更多内容:

column1.Width = new GridLength(1, GridUnitType.Auto); // Auto
column2.Width = new GridLength(1, GridUnitType.Star); // *
Run Code Online (Sandbox Code Playgroud)