MobileToolGrid.xaml.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. namespace InABox.Mobile
  9. {
  10. public class ToolGridViewModel : BindableObject
  11. {
  12. public IList<IToolItem> Items { get; private set; }
  13. public IList<IToolItem> VisibleItems { get; private set; }
  14. public ToolGridViewModel()
  15. {
  16. Items = new ObservableCollection<IToolItem>();
  17. VisibleItems = new ObservableCollection<IToolItem>();
  18. ((ObservableCollection<IToolItem>)Items).CollectionChanged += (sender, args) =>
  19. {
  20. VisibleItems.Clear();
  21. foreach (var item in Items.Where(x=>x.IsVisible))
  22. VisibleItems.Add(item);
  23. };
  24. }
  25. public Color BackgroundColor { get; set; }
  26. }
  27. [XamlCompilation(XamlCompilationOptions.Compile)]
  28. public partial class MobileToolGrid
  29. {
  30. public IList<IToolItem> Items => _viewModel.Items;
  31. public MobileToolGrid()
  32. {
  33. InitializeComponent();
  34. BindableLayout.SetItemsSource(_flexgrid, _viewModel.VisibleItems);
  35. }
  36. private void ImageTapped(object sender, EventArgs e)
  37. {
  38. if ((sender is Image image) && (image.BindingContext is MobileToolItem item) && item.IsEnabled)
  39. item.DoTap();
  40. }
  41. }
  42. }