I want to create a TextBox that can take measurement and convert it to different units if necessary (the end result being of type double
). The conversion will be controlled by a value IsMetric
. If IsMetric == true
then "36.5 in"
would turn into 927.1
(a double representing millimeters). Conversely, if IsMetric == false
then "927.1 mm"
would turn into 36.5
.
I thought to use an IValueConverter
on a regular TextBox
, but the ConverterParameter
is not a DependencyProperty
and therefore I can't bind IsMetric
to it.
I tried IMultiValueConverter
but the ConvertBack
function only receives the current value of the TextBox
and not all the bound values. This means I don't know IsMetric
when converting the user input.
Have I missed something with the ConvertBack
function? If not, then do I need to create a class derived from TextBox
?
I ended up with something along these lines for now. Would still enjoy a solution that doesn't require a DataTrigger for every possible value.
It's a bit different than the answer posted by @SamTheDev but along the same lines.
xaml
xaml.cs
Converter
Usage: