NumericUpDownPage.xaml.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using Avalonia.Controls;
  6. using Avalonia.Data.Converters;
  7. using Avalonia.Markup.Xaml;
  8. using MiniMvvm;
  9. namespace ControlCatalog.Pages
  10. {
  11. public partial class NumericUpDownPage : UserControl
  12. {
  13. public NumericUpDownPage()
  14. {
  15. InitializeComponent();
  16. var viewModel = new NumbersPageViewModel();
  17. DataContext = viewModel;
  18. }
  19. public static IValueConverter CultureConverter =
  20. new FuncValueConverter<CultureInfo, NumberFormatInfo>(c => (c ?? CultureInfo.CurrentCulture).NumberFormat);
  21. }
  22. public class NumbersPageViewModel : ViewModelBase
  23. {
  24. private IList<FormatObject>? _formats;
  25. private FormatObject? _selectedFormat;
  26. private IList<Location>? _spinnerLocations;
  27. private double _doubleValue;
  28. private decimal _decimalValue;
  29. public NumbersPageViewModel()
  30. {
  31. _selectedFormat = Formats.FirstOrDefault();
  32. }
  33. public double DoubleValue
  34. {
  35. get { return _doubleValue; }
  36. set { this.RaiseAndSetIfChanged(ref _doubleValue, value); }
  37. }
  38. public decimal DecimalValue
  39. {
  40. get { return _decimalValue; }
  41. set { this.RaiseAndSetIfChanged(ref _decimalValue, value); }
  42. }
  43. public IList<FormatObject> Formats
  44. {
  45. get
  46. {
  47. return _formats ?? (_formats = new List<FormatObject>()
  48. {
  49. new FormatObject() {Name = "Currency", Value = "C2"},
  50. new FormatObject() {Name = "Fixed point", Value = "F2"},
  51. new FormatObject() {Name = "General", Value = "G"},
  52. new FormatObject() {Name = "Number", Value = "N"},
  53. new FormatObject() {Name = "Percent", Value = "P"},
  54. new FormatObject() {Name = "Degrees", Value = "{0:N2} °"},
  55. });
  56. }
  57. }
  58. public IList<Location> SpinnerLocations
  59. {
  60. get
  61. {
  62. if (_spinnerLocations == null)
  63. {
  64. _spinnerLocations = new List<Location>();
  65. foreach (Location value in Enum.GetValues(typeof(Location)))
  66. {
  67. _spinnerLocations.Add(value);
  68. }
  69. }
  70. return _spinnerLocations ;
  71. }
  72. }
  73. // Trimmed-mode friendly where we might not have cultures
  74. public IList<CultureInfo?> Cultures { get; } = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
  75. .Where(c => new[] { "en-US", "en-GB", "fr-FR", "ar-DZ", "zh-CH", "cs-CZ" }.Contains(c.Name))
  76. .ToArray();
  77. public FormatObject? SelectedFormat
  78. {
  79. get { return _selectedFormat; }
  80. set { this.RaiseAndSetIfChanged(ref _selectedFormat, value); }
  81. }
  82. }
  83. public class FormatObject
  84. {
  85. public string? Value { get; set; }
  86. public string? Name { get; set; }
  87. }
  88. }