none
xml with stringformat currency

    Question

  • Hello,

    I have a question. I'm trying to populate listview with xml document. one of the xml node is money/currency. I've tried using StringFormat but it's doesn't seem to work. I found out stringFormat only works on integer/double etc. Any suggestion how to read xml and make it currency format ?

    Thanks for the help

    Tuesday, August 10, 2010 11:42 AM

Answers

  • Hi,

    As I have mentioned before, since the node Wage is a string type, you need to convert first the string to a double using the IValueConverter.

    Your XAML should look like this.

     <GridViewColumn Header="Wage" Width="100" DisplayMemberBinding="{Binding XPath=Wage, Converter={StaticResource myConverter}, StringFormat='{}{0:C}'}">

     

    and your converter should be look like this but you can also change.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Data;
    
    namespace WpfApplicationXML
    {
      public class StringToDoubleConverter : IValueConverter
      {
        #region IValueConverter Members
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          double nValue = 0;
          if (value != null)
          {
            nValue = System.Convert.ToDouble(value);
          }
          return nValue;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          double nValue = 0;
          if (value != null)
          {
            nValue = System.Convert.ToDouble(nValue);
          }
          return nValue;
        }
    
        #endregion
      }
    }
    
    

    you also need to define your local resource for your converter.

    <local:StringToDoubleConverter x:Key="myConverter" />

     

    Hope this helps.

    Regards,

    gioVhan

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Parry Chan Wednesday, August 11, 2010 9:20 AM
    Wednesday, August 11, 2010 8:02 AM

All replies

  • Hi,

    Since that is a string you will have to use converter (IValueConverter).

    Please see this link.

    http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

     

    Hope this helps.

    Regards,

    gioVhan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, August 10, 2010 12:46 PM
  • Hi gioVhan... so there's no way i could use string format for my xml file ?

    i thought since string format works on database field, it can work on xml to.

    Thanks..

    Tuesday, August 10, 2010 1:13 PM
  • Hi,

    You can still use the stringFormat.  You might not even use the converter after all.

    Could you post some code on how you format it?

    Here's some link for your reference.

    http://blogs.msdn.com/b/vinsibal/archive/2008/05/16/new-wpf-sp1-feature-data-formatting.aspx

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/707768f8-5950-4372-8e15-c7ec9c0abb14

     

    Regards,

    gioVhan



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, August 10, 2010 3:09 PM
  • this is what I have tried so far :
    <Window.Resources>
    <XmlDataProvider x:Key="GroupDS" d:IsDataSource="True" 
     Source="XMLFile1.xml" XPath="Group/Data"/>
    </Window.Resources>
     <Grid>
     <ListView Margin="18,29,21,0" x:Name="listView1" Height="100" 
      VerticalAlignment="Top" ItemsSource="{Binding Mode=Default, 
      Source={StaticResource GroupDS}, XPath=/Group/Data}">
      <ListView.View>
      <GridView>
       <GridViewColumn Header="Name" Width="100" 
       DisplayMemberBinding="{Binding XPath=Name}"></GridViewColumn>
       <GridViewColumn Header="Wage" Width="100" 
      DisplayMemberBinding="{Binding XPath=Wage , StringFormat='{}{0:C}'}">
       </GridViewColumn
      </GridView>
      </ListView.View>
     </ListView>
     </Grid>
    
    

    the xml file :

    <?xml version="1.0" encoding="utf-8" ?>
    <Group>
    <Data>
     <Name>John</Name>
     <Wage>1000</Wage>
    </Data>
    <Data>
     <Name>Andy</Name>
     <Wage>1500</Wage>
    </Data>
    </Group>
    
    

    I want to display my wage to a currency is there a way to do it ?

    Thanks

    Tuesday, August 10, 2010 3:56 PM
  • Hi,

    As I have mentioned before, since the node Wage is a string type, you need to convert first the string to a double using the IValueConverter.

    Your XAML should look like this.

     <GridViewColumn Header="Wage" Width="100" DisplayMemberBinding="{Binding XPath=Wage, Converter={StaticResource myConverter}, StringFormat='{}{0:C}'}">

     

    and your converter should be look like this but you can also change.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Data;
    
    namespace WpfApplicationXML
    {
      public class StringToDoubleConverter : IValueConverter
      {
        #region IValueConverter Members
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          double nValue = 0;
          if (value != null)
          {
            nValue = System.Convert.ToDouble(value);
          }
          return nValue;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          double nValue = 0;
          if (value != null)
          {
            nValue = System.Convert.ToDouble(nValue);
          }
          return nValue;
        }
    
        #endregion
      }
    }
    
    

    you also need to define your local resource for your converter.

    <local:StringToDoubleConverter x:Key="myConverter" />

     

    Hope this helps.

    Regards,

    gioVhan

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Parry Chan Wednesday, August 11, 2010 9:20 AM
    Wednesday, August 11, 2010 8:02 AM
  • Hi, It works great.... I have another question. the currency i got is $, can i change to other currency ??

    Thanks

    Wednesday, August 11, 2010 10:16 AM