MobileToolGrid.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using Xamarin.Forms;
  7. using Xamarin.Forms.Xaml;
  8. using XF.Material.Forms;
  9. namespace InABox.Mobile
  10. {
  11. public class ToolGridLayoutChangedEventArgs : EventArgs
  12. {
  13. public int RowCount { get; private set; }
  14. public int ColCount { get; private set; }
  15. public ToolGridLayoutChangedEventArgs(int rowcount, int colcount)
  16. {
  17. RowCount = rowcount;
  18. ColCount = colcount;
  19. }
  20. }
  21. public delegate void ToolGridLayoutChangedEvent(object sender, ToolGridLayoutChangedEventArgs args);
  22. public class ToolGridViewModel : BindableObject
  23. {
  24. public IList<IMobileToolItem> Items { get; private set; }
  25. public IList<IMobileToolItem> VisibleItems { get; private set; }
  26. public int Columns { get; set; }
  27. public ToolGridViewModel()
  28. {
  29. Columns = 4;
  30. Items = new ObservableCollection<IMobileToolItem>();
  31. VisibleItems = new ObservableCollection<IMobileToolItem>();
  32. ((ObservableCollection<IMobileToolItem>)Items).CollectionChanged += (sender, args) =>
  33. {
  34. VisibleItems.Clear();
  35. DoLayout();
  36. };
  37. }
  38. private void DoLayout()
  39. {
  40. int iRow = 0;
  41. int iCol = 0;
  42. foreach (var item in Items.Where(x => x.IsVisible))
  43. {
  44. item.Row = iRow;
  45. item.Column = iCol;
  46. VisibleItems.Add(item);
  47. iCol++;
  48. if (iCol == Columns)
  49. {
  50. iRow++;
  51. iCol = 0;
  52. }
  53. }
  54. LayoutChanged?.Invoke(this,new ToolGridLayoutChangedEventArgs(iRow,Columns));
  55. }
  56. public Color BackgroundColor { get; set; }
  57. public event ToolGridLayoutChangedEvent LayoutChanged;
  58. }
  59. [XamlCompilation(XamlCompilationOptions.Compile)]
  60. public partial class MobileToolGrid
  61. {
  62. public IList<IMobileToolItem> Items => _viewModel.Items;
  63. public MobileToolGrid()
  64. {
  65. InitializeComponent();
  66. BindableLayout.SetItemsSource(_flexgrid, _viewModel.VisibleItems);
  67. }
  68. private readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  69. nameof(BorderColor),
  70. typeof(Color),
  71. typeof(MobileToolGrid),
  72. Material.Color.SecondaryVariant
  73. );
  74. public Color BorderColor
  75. {
  76. get => (Color)GetValue(BorderColorProperty);
  77. set => SetValue(BorderColorProperty, value);
  78. }
  79. private void ImageTapped(object sender, EventArgs e)
  80. {
  81. if ((sender is Image image) && (image.BindingContext is MobileMobileToolItem item) && item.IsEnabled)
  82. item.DoTap();
  83. }
  84. private void _viewModel_OnLayoutChanged(object sender, ToolGridLayoutChangedEventArgs args)
  85. {
  86. _flexgrid.RowDefinitions.Clear();
  87. for (int i=0; i< args.RowCount; i++)
  88. _flexgrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto});
  89. _flexgrid.ColumnDefinitions.Clear();
  90. for (int i=0; i< args.ColCount; i++)
  91. _flexgrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star});
  92. }
  93. }
  94. }