简体   繁体   中英

Converting numbers to string using IValueConverter or string property in viewmodel?

I have several Labels in my View.xaml that display some kind number (eg <Label Text="{Binding Quantity}"/> ). These are backed in the Model.cs by Properties (eg public decimal OnHandQuantity { get; set; } ). And exposed through the ViewModel.cs (eg public decimal Quantity { get; set; } ).

I now want the labels to display the text in a specific format, depending on which localization the user chooses in the settings or even a custom format.

What would be the prefered method to achieve this?

I currently pend between two methods: Changing the ViewModel.Quantity to a string and formatting it when i set it (eg item.Quantity = _stocks[0].OnHandQuantity.ToString("N", App.Settings.Language.NumberFormat); ). Or i could implement a DecimalToStringConverter : IValueConverter and use it for the labels that show numbers.

What are the advantages and disadvantages of my proposed solutions?

What are the advantages and disadvantages of your proposed solutions?

Actually , these two ways are all wisely. However,if you have other logic about the text of labels,binding the value in ViewModel is better.Otherwise you could set the BindingMode as TwoWay and get it in your ViewModel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM