如何在代码后台中表示XAML中的<Button Height="Auto"/>?

36
你怎样在代码后端中将 Height="*"Height="Auto" 设定好并使其生效?
1个回答

60

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

示例:

element.Height = double.NaN;

设置 Width/Height = "*" (仅适用于一些特定元素,例如ColumnDefinitionRowDefinition)与其他情况略有不同。 Width/Height 值的类型是GridLength,而非 double

例如(更多示例可在 此 MSDN 页面 上找到):

column1.Width = new GridLength(1, GridUnitType.Auto); // Auto
column2.Width = new GridLength(1, GridUnitType.Star); // *

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接