2

I saw this post: How to Conditionally Format a String in .Net?

The first part of the question points to the ability to use conditional formats. How is this done?

In my case, I want to do for instance 100,000 as $100k, and 1,000,000 as $1m. I want to be able to do it with just the FormatString in markup (Silverlight). This is a case where I cant use a value converter (it's inside a style).

Is this possible?

2
  • You need to code it yourself. Check out: stackoverflow.com/questions/2134161/… Commented Dec 31, 2010 at 22:34
  • what does this refer to in that post I linked to? Int32 i = 0; i.ToString("$#,##0.00;($#,##0.00);Zero"); Commented Dec 31, 2010 at 22:50

1 Answer 1

4

You can implement your own IFormatProvider and define ie. custom and pass that when calling String.Format() or ToString().

Example of this can be found here http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx or here http://www.codeproject.com/KB/cs/custstrformat.aspx.

public class StringFormatInfo : IFormatProvider, ICustomFormatter
{
   ...
}

return number.ToString("{0:custom}", new StringFormatInfo());
Sign up to request clarification or add additional context in comments.

3 Comments

i've used it alot myself... its a clean nice api instead of using all kinds of helper methods.
I don't think this is an option for me because I am using a template binding. I need this inside a style that is applied to the label on the axis of a chart (Silverlight Toolkit). I'll give it a try.
There is an article here about how to use custom IFormatProvider's when databinding in WPF timheuer.com/blog/archive/2008/07/30/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.