NumericUpDownPage.xaml.cs 3.0 KB

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