C# 如何在后面的代码中说 XAML <Button Height="Auto"/>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1497921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to say XAML <Button Height="Auto"/> in code behind?
提问by Edward Tanguay
How can you set Height="*"and Height="Auto"in code behind?
你怎么能在后面的代码中设置Height="*"和Height="Auto"?
采纳答案by Noldorin
For setting Height = "Auto"on most controls, you want to assign the value with double.NaN.
对于Height = "Auto"大多数控件的设置,您希望使用 分配值double.NaN。
Example:
例子:
element.Height = double.NaN;
Setting Width/Height = "*"( is a slightly different matter, since it only applies to a select few elements (ColumnDefinitionand RowDefinitionfor example). The type of the Width/Heightvalue is GridLength, rather than double.
设置Width/Height = "*"(是一个稍微不同的问题,因为它只适用于有选择的几个元件(ColumnDefinition和RowDefinition例如)。该类型的Width/Height值GridLength,而不是double。
Example (more are given on this MSDN page:
示例(在此 MSDN 页面上提供了更多信息:
column1.Width = new GridLength(1, GridUnitType.Auto); // Auto
column2.Width = new GridLength(1, GridUnitType.Star); // *

