Question

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.

Was it helpful?

Solution

It turns out that my string FormatNumberOfDecimalsshould be of format {0:0.0000} instead of 0.00000.

0.0000 works on ContentStringFormat of a Label and does not cause Exceptions in my Visual Studio Designer but apparently does not work on a TextBox when using String.Format().

{0:0.0000} works on ContentStringFormat of a Label and on my TextBox using String.Format(). Unfortunately, my Visual Studio Design view is now throwing an exception and of no use for the View with my TextBox.

System.FormatException
Input string was not in a correct format.
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at System.String.Format(String format, Object arg0)

OTHER TIPS

I've had problems like this in the past too, where a MultiConverter will not automatically update when one of the bound values updates.

Since you are not using a binding for your 2nd value, you can switch to a single IValueConverter, and pass your format in as the ConverterParameter

Or simply use the StringFormat property of the binding

<TextBox Text="{Binding X, StringFormat=D4}" />

Note that if you are binding the Content property instead of the Text property, such as on a Label, the StringFormat property of the binding won't work and you need to set the Label's ContentStringFormat property instead.

that is work

decimal debit = 0; decimal credit = 0;

        decimal.TryParse(values[0].ToString(), out debit);
        decimal.TryParse(values[1].ToString(), out credit);

        decimal total = debit - credit;

        return total.ToString("0.00"); // string form 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top