将控件的高度绑定到特定于单位的测量值
我知道在 WPF 中您可以以不同的单位指定控件的高度。
例如:
<Setter Property="Height" Value="0.35cm"></Setter>
我想将控件的高度绑定到 ViewModel 中的属性。使用绑定时如何指定高度以厘米为单位?我尝试将 ViewModel 中的 Height 属性设置为字符串,并在高度测量后面附加“cm”:
ViewModel 在 XAML 中创建,如下所示:
<local:HeadingViewModel Height="0.35cm"></local:HeadingViewModel>
控件的高度是通过样式中的绑定设置的:
<Setter Property="Height" Value="{Binding Height, RelativeSource={RelativeSource Mode=TemplatedParent}}"></Setter>
这设置了高度,但不是以厘米为单位。好像是WPF默认单位设置的。当我将高度从“0.35cm”更改为“0.35in”时,没有任何反应。它似乎保留在WPF的默认单位中。
如何通过绑定以不同于默认单位的测量单位设置控件的高度属性?
I am aware that in WPF you can specify the Height of a Control in different units.
For example:
<Setter Property="Height" Value="0.35cm"></Setter>
I want to bind the Height of a Control to a property in my ViewModel. How do I specify that the height is in centimeters when using a binding? I have tried to set the Height property in the ViewModel to a String and append "cm" behind the height measurement:
The ViewModel is created in XAML as follows:
<local:HeadingViewModel Height="0.35cm"></local:HeadingViewModel>
The Height of the Control is set via a binding in the style:
<Setter Property="Height" Value="{Binding Height, RelativeSource={RelativeSource Mode=TemplatedParent}}"></Setter>
This sets the height, but it is not in centimeters. It seems to be set in the default units of WPF. Nothing happens when I change the height from "0.35cm" to "0.35in". It seems to stay in the default unit of WPF.
How can I set the Height property of a Control via Binding in a different measurement unit than the default unit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 WPF 中并没有真正使用不同单位进行绑定的概念 - 所有的都是一个
TypeConverter
-LengthConverter
- 它将 XAML 提供的字符串转换为 <代码>双。根据您输入的单位,它会将结果乘以某个因子。因此,如果您想绑定到双精度型,则必须确保将代码中的双精度型转换为 WPF 的 1/94 英寸单位。或者您可以制作一个
IValueConverter
来为您完成此操作。如果您想绑定到字符串,您可以编写一个
IValueConverter
并从内部调用LengthConverter
。There isn't really a concept of binding using different units in WPF - all there is, is a
TypeConverter
-LengthConverter
- which converts a string as provided by XAML into adouble
. Depending on the units you enter, it multiplies the result by a certain factor.So if you want to bind to a double, you have to make sure you convert that double in code to WPF's units of 1/94th of an inch. Or you could make an
IValueConverter
to do it for you.If you want to bind to a string, you could write an
IValueConverter
and callLengthConverter
from within.