I am using Caliburn.Micro.
In my View I have a TextBox Binded to double X and a Button which alters the value of X in my ViewModel.
public void ButtonPressed()
{
X = AnObject.GetDouble;
NotifyOfPropertyChange(() => X);
}
public double X { get; set; }
My Goal is to limit the number of decimals that is displayed. This number, is configurable in the application and therefore available as a Property of AnObject. Therefore I have defined a IMultiValueConverter:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return String.Format(values[1].ToString(), values[0]);
}
and defined my TextBox as follows:
<TextBox>
<TextBox.Text>
<MultiBinding Converter="{StaticResource coordinateConverter}" >
<Binding Path="X" />
<Binding Path="AnObject.FormatNumberOfDecimals" />
</MultiBinding>
</TextBox.Text>
</TextBox>
What works: The initial value of X, zero, is being formatted correctly.
What doesn’t work: When the button is pressed, the new value is not being displayed in the TextBox.
Suggestions that don’t use IMultiValueConverter are welcome as well.
It turns out that my
string FormatNumberOfDecimalsshould be of format{0:0.0000}instead of0.00000.0.0000works on ContentStringFormat of a Label and does not cause Exceptions in my Visual Studio Designer but apparently does not work on a TextBox when usingString.Format().{0:0.0000}works on ContentStringFormat of a Label and on my TextBox usingString.Format(). Unfortunately, my Visual Studio Design view is now throwing an exception and of no use for the View with my TextBox.