Reputation: 31
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?
Upvotes: 2
Views: 352
Reputation: 21713
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 a double
. 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 call LengthConverter
from within.
Upvotes: 1