How can I format a decimal bound to TextBox without angering my users?

前端 未结 3 1026
陌清茗
陌清茗 2020-12-23 19:20

I\'m trying to display a formatted decimal in a TextBox using data binding in WPF.

Goals

Goal 1: When setting a decimal property in code, display 2 decimal p

3条回答
  •  暖寄归人
    2020-12-23 20:06

    Try to resolve that on ViewModel level. That it:

    public class FormattedDecimalViewModel : INotifyPropertyChanged
        {
            private readonly string _format;
    
            public FormattedDecimalViewModel()
                : this("F2")
            {
    
            }
    
            public FormattedDecimalViewModel(string format)
            {
                _format = format;
            }
    
            private string _someDecimalAsString;
            // String value that will be displayed on the view.
            // Bind this property to your control
            public string SomeDecimalAsString
            {
                get
                {
                    return _someDecimalAsString;
                }
                set
                {
                    _someDecimalAsString = value;
                    RaisePropertyChanged("SomeDecimalAsString");
                    RaisePropertyChanged("SomeDecimal");
                }
            }
    
            // Converts user input to decimal or initializes view model
            public decimal SomeDecimal
            {
                get
                {
                    return decimal.Parse(_someDecimalAsString);
                }
                set
                {
                    SomeDecimalAsString = value.ToString(_format);
                }
            }
    
            // Applies format forcibly
            public void ApplyFormat()
            {
                SomeDecimalAsString = SomeDecimal.ToString(_format);
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void RaisePropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    

    SAMPLE

    Xaml:

    
    

    Code behind:

    public MainWindow()
    {
        InitializeComponent();
        FormattedDecimalViewModel formattedDecimalViewModel = new FormattedDecimalViewModel { SomeDecimal = (decimal)2.50 };
        tb.LostFocus += (s, e) => formattedDecimalViewModel.ApplyFormat(); // when user finishes to type, will apply formatting
        DataContext = formattedDecimalViewModel;
    }
    

提交回复
热议问题