如何在代码隐藏中将 FrameworkElement.Width 属性设置为 QualifiedDouble 的值?
我正在尝试将我的一个控件的宽度属性设置为qualifiedDouble,
如 MSDN 上所述。
(向下滚动到“XAML 值”部分以查看 MSDN 有关使用qualifiedDouble 的信息)
但是,我想知道如何在代码隐藏中实现此目的,而不是在 XAML 中。 出于继承目的,我创建的 UserControls 没有附加 XAML。 因此,我必须使用 C# 中的任何功能手动执行所有 XAML 操作。
有谁知道qualifiedDouble是如何在代码隐藏中实现的?
I'm trying to set the width property of one of my controls to a qualifiedDouble,
as described here on MSDN.
(Scroll down to the "XAML Values" section to see MSDN's info on the use of a qualifiedDouble)
However, I want to know how to achieve this in the code-behind, rather than XAML. The UserControls I am creating do not have XAML attached to them, for inheritance purposes. So I have to perform all XAML operations manually, using whatever I can in C#.
Does anyone know how qualifiedDouble is achieved in the code-behind?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
真是巧合,我今天早些时候必须这样做。 合格的双打最终会根据您给定的单位进行因子转换,但作为
LengthConverter
的一部分。替代:
例如,这会将“10pt”转换为 13.3333333。 您还可以使用文章提供的转换因子,但我更喜欢使用上面的转换因子,因为这些因子是内置在类中的。
已编辑:响应有关字符串的评论...
字符串转换对其用途来说非常有意义。 他们使用 XAML 是因为用 XAML 表达某些内容比用 C# 或 VB 容易得多。 在 XAML 中,所有值都是字符串,因此您会自动选择
TypeConverter
将字符串转换为目标类型。 例如,FontSizeConverter
调用LengthConverter
上的内部静态方法。 (您也可以实例化FontSizeConverter
。)还有GridLength
的转换器,例如“4*”和Width
的转换器,例如“Auto”。 或者,就像我说的,您可以创建自己的类来进行不带字符串的转换。本文建议,对于代码隐藏,直接使用该因子,因此我在上面提供了一个替代示例。
What coincidence, I had to do this earlier today. The qualified doubles end up going through a factor conversion based on the unit you give it, but as part of
LengthConverter
.Alternate:
This will transform "10pt" to 13.3333333, for example. You could also use the conversion factors the article supplies, but I prefer to use the above since the factors are built into the class.
Edited: In response to comment about strings...
String conversion makes perfect sense for what it was intended for. They use XAML because it is so much easier to express some things in XAML than in C# or VB. In XAML, all the values are strings, so you have
TypeConverter
s automatically selected to convert the string to the target type.FontSizeConverter
for example, calls an internal static method onLengthConverter
. (You could also instantiateFontSizeConverter
instead.) There are also converters forGridLength
s like "4*" andWidth
s like "Auto". Or, like I said, you can create your own class to convert without strings.This article recommends, for code-behind, to use the factor directly, so I supplied an alternate example above.