我想我的一个控件的宽度属性设置为qualifiedDouble, 描述这里的MSDN 。 (向下滚动到XAML值部分看到使用qualifiedDouble的MSDN的信息)
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)
不过,我想知道如何实现这一目标,在code-后面,而不是XAML。我创建的用户控件不具备XAML重视他们,继承的目的。所以我必须手动执行所有的XAML操作,使用任何我可以在C#。
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#.
有谁知道如何qualifiedDouble是在$ C $取得了C-后面?
Does anyone know how qualifiedDouble is achieved in the code-behind?
什么巧合,我有这个今天早些时候做。合格的双打最终会通过基于你给它单元上的因素的转换,但由于部分 LengthConverter
。
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
.
LengthConverter lc = new LengthConverter();
string qualifiedDouble = "10pt";
double converted = lc.ConvertFrom( qualifiedDouble );
备选:
double original = 10.0;
double converted = original * 1.333333333; // px-to-pt conversion
这将改变10PT为13.3333333为例。你也可以使用转换系数的文章用品,但我preFER使用上述自从因素都内置到类。
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...
字符串转换非常有意义的东西它的目的是为。他们使用XAML,因为它是如此容易得多EX preSS有些东西在XAML比C#或VB。在XAML中,所有的值都是字符串,所以你必须类型转换器
S也可以自动选择将字符串转换为目标类型。 FontSizeConverter
为例,呼吁 LengthConverter
内部的静态方法。 (你也可以实例 FontSizeConverter
代替。)还有一些转换器 GridLength
就像4 *和宽度
就像自动。或者,就像我说的,你可以创建自己的类来没有字符串转换。
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 on LengthConverter
. (You could also instantiate FontSizeConverter
instead.) There are also converters for GridLength
s like "4*" and Width
s like "Auto". Or, like I said, you can create your own class to convert without strings.
这篇文章建议,为code-身后,用直接的因素,所以我上面提供一个备用的例子。
This article recommends, for code-behind, to use the factor directly, so I supplied an alternate example above.