Iyon_Groznyy

Untitled

Oct 4th, 2021
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. public partial class SelectableRowComponent : Grid
  2.     {
  3.         private ViewBinder<ISelectableItemViewModel> _viewBinder;
  4.  
  5.         public SelectableRowComponent()
  6.         {
  7.             InitializeComponent();
  8.  
  9.             _viewBinder = this.CreateViewBinder<ISelectableItemViewModel>(Bind);
  10.         }
  11.  
  12.         private bool _isSelected;
  13.         public bool IsSelected
  14.         {
  15.             get => _isSelected;
  16.             set
  17.             {
  18.                 _isSelected = value;
  19.                 OnIsSelectedChanged(_isSelected);
  20.             }
  21.         }
  22.  
  23.         private void Bind()
  24.         {
  25.             using var bindingSet = _viewBinder.CreateBindingSet();
  26.  
  27.             bindingSet.Bind(this).For(v => v.BindTap()).To(vm => vm.SelecteCommand);
  28.             bindingSet.Bind(this).For(v => v.IsSelected).To(vm => vm.IsSelected);
  29.             bindingSet.Bind(RowImage).For(v => v.Source).To(vm => vm.Icon);
  30.             bindingSet.Bind(RowImage).For(v => v.BindTintColor()).To(vm => vm.IsSelected)
  31.                       .WithBoolToColorConversion(AppColor.Dark, AppColor.Blue);
  32.             bindingSet.Bind(TextLabel).For(v => v.Text).To(vm => vm.Title);
  33.             bindingSet.Bind(CountFrame).For(v => v.IsVisible).To(vm => vm.Count)
  34.                       .WithConversion((int? count) => count is not null && count > 0);
  35.             bindingSet.Bind(CountLabel).For(v => v.Text).To(vm => vm.Count);
  36.             bindingSet.Bind(IsSelectedImage).For(v => v.IsVisible).To(vm => vm.IsSelected);
  37.         }
  38.  
  39.         private void OnIsSelectedChanged(bool isSelected)
  40.         {
  41.             if (isSelected)
  42.             {
  43.                 TextLabel.TextColor = AppColor.Blue.ToFormsColor();
  44.                 CountFrame.BackgroundColor = AppColor.Blue.ToFormsColor();
  45.                 return;
  46.             }
  47.  
  48.             TextLabel.TextColor = _viewBinder.ViewModel.Count == 0 ? AppColor.DarkLight.ToFormsColor() : AppColor.DarkDeep.ToFormsColor();
  49.             CountFrame.BackgroundColor = AppColor.DarkLight.ToFormsColor();
  50.         }
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment