Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
665
percent conversion with xamnumeric editor
posted

Does anyone have an example with percent conversion with xamnumeric editor? Converting to/from .xx (database) to xx% .

  • 54937
    Offline posted

    When you put a binding on the Value property to bind to your underling object property, you can set the Converter to a class that you create that implements IValueConverter. Here's a simple version that uses 2 numeric editors:

        <StackPanel>
            <StackPanel.Resources>
                <local:PercentConverter x:Key="PctConverter" />
            </StackPanel.Resources>
            <igEditors:XamNumericEditor x:Name="originalPct" />
            <igEditors:XamNumericEditor 
                x:Name="numPercent" 
                Value="{Binding ElementName=originalPct, Path=Value, Converter={StaticResource PctConverter}}" />
        </StackPanel>

    "local" will need to be mapped to whatever clr namespace you put the IValueConverter in.

        public class PercentConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value == null)
                    return value;
     
                double dbl = ((IConvertible)value).ToDouble(culture);
                dbl *= 100d;
                return dbl;
            }
     
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value == null)
                    return value;
     
                double dbl = ((IConvertible)value).ToDouble(culture);
                dbl = dbl / 100d;
                return dbl;
            }
        }