NumericUpDownPage.xaml.cs 3.2 KB

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