NumericUpDownPage.xaml.cs 3.0 KB

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