0

In my application I am dealing with monetary value, yet being precise to the cent suffices. Hence I decided to store all amounts as integers in cents. But when I bind a value to Xaml, I'd like to see the value in dollars, using something like

<TextBlock Text="{Binding Product.Price, StringFormat={}{***}}"/> 

where *** should be replaced by something appropriate. I read through the documentation http://msdn.microsoft.com/en-us/library/vstudio/0c899ak8(v=vs.100).aspx

where there are examples of dividing by 1000 (using ','), multiplying by 100 (using '%') etc. Is there a way to do custom scaling (like divide by 100) in StringFormat, or does one really have to write a custom value converter?

2
  • How bout adding a property to the product which will format the internal value of Price? Commented Oct 23, 2015 at 9:45
  • I have more numbers per product, and also I'm storing them in a db. I only need this when displaying so I think it'd be wrong to add a property to the model as it is a concern of the view only. Commented Oct 23, 2015 at 9:47

2 Answers 2

3

Other than those operations you mention (scaling by 1000, or performing percent or perthousand conversion), there is no way to do "custom" scaling with format strings.

You can either solve it by using a Converter, or if you are going to use it somewhere else (and it's not a very specific case), you may want to add a property to your Product, something like:

decimal PriceInDollars { get { return Price / 100; } 

Remember to have your changes to Price notified for this property aswell if that's a need.

If you want to go the Converter route, and you want something versatile, you may want to try this: http://www.codeproject.com/Articles/239251/MathConverter-How-to-Do-Math-in-XAML or one of the many others scattered on the web (search for something like "Math Converter WPF", there are plenty). No need to write your own from scratch.

Using the one I linked (which I'm not associated to at all), something like:

<!-- somewhere in resources --> <ikriv:MathConverter x:Key="MathConverter" /> 

Then:

<TextBlock Text="{Binding Product.Price, Converter={StaticResource MathConverter}, ConverterParameter=x/100}"/> 
Sign up to request clarification or add additional context in comments.

5 Comments

As I commented above, there are lots of these numbers in my application, not only a product with a price - so I don't want to add many properties to my models. This should be solved in the view.
Then a Converter is your friend, see my edit for a link to something pretty versatile
Thx, I tried the converter in the link. Now I get the right numeric value (i.e. 17 becomes 0,17), but now I would like to display that as $0.17 ...
Add a StringFormat=\${0}, or StringFormat=C or whatever needed to the Binding
Or have the converter return a string with the dollar sign prefix, at least that way you can unit test the converter.
1

Following the suggestions I wrote a very simple ValueConverter, namely

public class CentConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (targetType != typeof(string)) throw new NotImplementedException(); var valueInCents = Int32.Parse(value.ToString()); return String.Format("{0:C}", (double)valueInCents / 100); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

Since I know I will always want to use it only in context of converting to a string, and I never need to convert back. It would be nice if there were strongly-typed version of converters (or better, more concretely-typed that to the type object), e.g., with method signaure as below so one could get rid of the casts,

public string Convert(int value, object parameter, CultureInfo culture) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.