C# WPF - GridLength GridUnitType.Auto
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17974536/
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
C# WPF - GridLength GridUnitType.Auto
提问by shann1990
Can anyone explain the difference between using:
任何人都可以解释使用之间的区别:
GridLength length = new GridLength(0, GridUnitType.Auto)
and
和
GridLength length = new GridLength(1, GridUnitType.Auto)
My limited knowledge of this leads me to believing these would both be identical solutions due to auto being as it states..."auto", therefore making the double value redundant.
我对此的有限了解使我相信这些都是相同的解决方案,因为 auto 就像它所说的那样......“自动”,因此使双值变得多余。
Most examples I have seen show the GridUnitType.Autobeing preceded with 1 rather than 0, but it seems to me that either option works the same?
我见过的大多数示例都显示GridUnitType.Auto前面是 1 而不是 0,但在我看来,这两个选项的工作原理相同吗?
Is this the case or can anyone shed some light on if/how these are different
是这种情况还是任何人都可以阐明这些是否/如何不同
回答by Chris
I think your understanding is correct, when the value GridUnitType.Autois used, the first value passed to the constructor is redundant, as the size will be determined by the content object.
我认为您的理解是正确的,当GridUnitType.Auto使用该值时,传递给构造函数的第一个值是多余的,因为大小将由内容对象决定。
It somewhat makes sense in the context of the GridLengthstructure constructor to retain this parameter (even though it's not used in this instance), as it allows the second parameter type to contain values that describe the all available states of GridUnitType.
在GridLength结构构造函数的上下文中保留这个参数(即使它没有在这个实例中使用)在某种程度上是有意义的,因为它允许第二个参数类型包含描述 的所有可用状态的值GridUnitType。
From the documentation:
从文档:
The enumerated type GridUnitTypecan contain the following values:
枚举类型GridUnitType可以包含以下值:
Auto - The size is determined by the size properties of the content object.
Pixel - The value is expressed as a pixel.
Star - The value is expressed as a weighted proportion of available space.
So really, the first parameter is only relevant when the second parameter is set to GridUnitType.Pixelor GridUnitType.Star.
所以实际上,第一个参数仅在第二个参数设置为GridUnitType.Pixelor时才相关GridUnitType.Star。
It wouldn't work neatly other way around e.g. if you tried to have constructor that accepted 1 parameter as a GridUnitType, and only required the second parameter if you used Pixelor Star.
它不会以其他方式巧妙地工作,例如,如果您尝试让构造函数接受 1 个参数作为 a GridUnitType,并且仅在您使用Pixelor时才需要第二个参数Star。
This way round, you get the benefit of having a 1 parameter constructor that accepts a double without specifying the additional type. Although it does have the cost of a potentially odd looking two parameter constructor when using Auto(as in your example).
通过这种方式,您可以获得一个 1 参数构造函数的好处,该构造函数接受双精度值而不指定附加类型。尽管它在使用时确实具有看起来很奇怪的两个参数构造函数的成本Auto(如您的示例中所示)。

