locked
Value Converter (Double to Currency) RRS feed

  • Question

  • First of all forgive me.  I know what I am trying to accomplish, but not all the proper terminology to explain it.

     

    I have some lists that are bound to XML data.  One of the columns displays data as a Double.  I would like to have that display as currency.  Example:  10000.00 shown as $10,000.00.  I found an int to double and back again example in the msdn library.  However, all my attempts to modify it for my needs have produced errors.

     

    public class DoubleToIntegerValueConverter : IValueConverter
     {
      public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
      {
       return System.Convert.ToInt32(value);
      }
    
      public object ConvertBack(object value, Type targetType,
       object parameter, System.Globalization.CultureInfo culture)
      {
       return System.Convert.ToDouble(value);
      }
    
     }
    
    

    How can I modify that converter class so it converts double to ToString("c")?

     

    Then I'd like to use it in that handy-dandy binding window, bottom-left, where I can add a value converter to my bound items.

    http://marksmarine.com/cal/ValueConverter.png

     

    Thanks!

    ~Christine

     

    How can I display a picure/img in these forums?  [img][/img] does not work.

    Saturday, June 25, 2011 6:42 PM

Answers

  • Sounds like the object being passed into the converter is not a double. Try this to find out what the type is:

     

    Put return value.ToString(); in the Convert method, then your display should show the type of the object being passed in, if it isn't double, what is it?

    Monday, June 27, 2011 6:36 PM
    Moderator
  • Even though Blend said it was a double, Blend lied.  It was a string.  I was able to get someone from the wpf forums to help me.

     

    Needed to change it to:

     

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
     double d = System.Convert.ToDouble(value); // This will succeed for doubles AND string values
     return d.ToString("c");
    }
    
    

    Thanks Chuck!

     

    ~Christine

    Monday, June 27, 2011 6:58 PM

All replies

  • You can use the html editor to include an image from another host, that is the only way I know how to do it.

     

    	public class DoubleToCurrencyConverter : IValueConverter
    	{
    		#region IValueConverter Members
    
    		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    		{
    			double d = (double) value;
    			return d.ToString("c");
    		}
    
    		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    		{
    			throw new NotImplementedException();
    		}
    
    		#endregion
    	}
    


     

    Monday, June 27, 2011 4:17 PM
    Moderator
  • While what you posted should work, however it doesn't. 

    I get the error... "InvalidCastException was unhandled by user code... Specified cast is not valid.

     

    Basically I have an XML document loaded into my Blend project with a description and a price.  They populate a list.

    I was using the value converter from within the template.  When I look at the Data tab where it shows my XML data, it shows ... "RetailPrice: (Double)".

    But all my attempts to convert double to... .ToString("c0") or .ToString("c") say I'm not doing it right or I am casting wrong.  Oh... guess I could just show you the code. :D

     

     

    <DataTemplate x:Key="SelOptionTemplate">
    			<StackPanel x:Name="optionPanel" Orientation="Vertical" MouseLeftButtonDown="addToOptionList" Background="#02FFFFFF" Width="280" Margin="0,0,0,10">
    				<StackPanel Height="30.18" Orientation="Horizontal">
    					<TextBlock x:Name="option" Text="{Binding XPath=OPTION}" Margin="0" Foreground="Navy" FontSize="13.333" Width="206" MaxWidth="206" MinWidth="206" TextWrapping="Wrap" d:LayoutOverrides="Height"/>
    					<TextBlock x:Name="retailPrice" Text="{Binding Converter={StaticResource DoubleToCurrencyConverter}, XPath=RetailPrice}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="62" TextAlignment="Right" FontSize="13.333" Foreground="Navy" Height="Auto" d:LayoutOverrides="VerticalAlignment"/>
    				</StackPanel>
    			</StackPanel>
    		</DataTemplate>
    

     

     

    I think it may have something to do with the binding to XML.


    Thanks as always and again Chuck.

     

    ~Christine


    Monday, June 27, 2011 4:53 PM
  • Sounds like the object being passed into the converter is not a double. Try this to find out what the type is:

     

    Put return value.ToString(); in the Convert method, then your display should show the type of the object being passed in, if it isn't double, what is it?

    Monday, June 27, 2011 6:36 PM
    Moderator
  • Even though Blend said it was a double, Blend lied.  It was a string.  I was able to get someone from the wpf forums to help me.

     

    Needed to change it to:

     

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
     double d = System.Convert.ToDouble(value); // This will succeed for doubles AND string values
     return d.ToString("c");
    }
    
    

    Thanks Chuck!

     

    ~Christine

    Monday, June 27, 2011 6:58 PM