Search code examples
silverlight-4.0decimalivalueconverter

Silverlight Converter to show decimals for two places


I want to display a decimal in a textbox with two decimals. When the page loads the value in the textbox is displayed with two decimals ("0.00") . When I change the value to say 10, it is just showing as 10. How can I show it as "10.00"

Following is my Converter.

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        decimal convertedBudget;
        if (value == null)
        {
            convertedBudget = 0.0M;
        }
        else
        {
            convertedBudget = (decimal)value;
        }
        return string.Format("{0:#,0.00}", Math.Round(convertedBudget, 2));
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        decimal convertedBudget = 0;
        if(value!=null && !string.IsNullOrEmpty(value.ToString()))
        {
           convertedBudget =  System.Convert.ToDecimal(value.ToString());
        }
        return Math.Round(convertedBudget, 2);
    }

Thanks in advance


Solution

  • You don't need ValueConverter for this, just bind TextBox.Text to your decimal and use string formatting on the TextBox itself. If you want to update value after entering text then capture appropriate event and change it.

    <UserControl x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400"
    xmlns:local="clr-namespace:SilverlightApplication2">
    <UserControl.DataContext>
        <local:VM x:Name="VM"/>
    </UserControl.DataContext>
        <Grid x:Name="LayoutRoot" Background="White">
        <TextBox Text="{Binding MyValue, Mode=TwoWay, StringFormat=\{0:0.00\}}" LostFocus="TextBox_LostFocus_1" />
        <Slider HorizontalAlignment="Left" Margin="75,189,0,0" VerticalAlignment="Top" Value="{Binding MyValue, Mode=TwoWay}" Width="293"/>
    </Grid>
    


    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
    
        private void TextBox_LostFocus_1(object sender, RoutedEventArgs e)
        {
            var v = (VM)this.DataContext;
            v.MyValue = Convert.ToDecimal(((TextBox)sender).Text);
        }
    }
    

    public class VM : INotifyPropertyChanged
    {
        public VM()
        {
    
        }
    
        private decimal myValue;
        public decimal MyValue
        {
            get { return myValue; }
            set { myValue = value; OnChanged("MyValue"); }
        }
    
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnChanged(string pName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(pName));
        }
    }