Guest User

Untitled

a guest
Jan 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using SegmentedControlSample.Controls;
  5. using Xamarin.Forms;
  6.  
  7. namespace SegmentedControlSample
  8. {
  9. public class SegmentedBarControl: ContentView
  10. {
  11. public static readonly BindableProperty ItemSelectedProperty = BindableProperty.Create(nameof(ItemSelected), typeof(string), typeof(SegmentedBarControl), null);
  12. public static readonly BindableProperty ChildrenProperty = BindableProperty.Create(nameof(Children), typeof(List<string>), typeof(SegmentedBarControl), null);
  13. public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(SegmentedBarControl), Color.DarkGray);
  14. public static readonly BindableProperty SelectedLineColorProperty = BindableProperty.Create(nameof(SelectedLineColor), typeof(Color), typeof(SegmentedBarControl), Color.Black);
  15. public static readonly BindableProperty SelectedTextColorProperty = BindableProperty.Create(nameof(SelectedTextColor), typeof(Color), typeof(SegmentedBarControl), Color.Black);
  16. public static readonly BindableProperty AutoScrollProperty = BindableProperty.Create(nameof(AutoScroll), typeof(bool), typeof(SegmentedBarControl), true);
  17. public delegate void ValueChangedEventHandler(object sender, EventArgs e);
  18. public event ValueChangedEventHandler ValueChanged;
  19. public string ItemSelected { get { return (string)GetValue(ItemSelectedProperty);} set { SetValue(ItemSelectedProperty, value); ValueChanged(this, new EventArgs()); } }
  20. public List<string> Children { get { return (List<string>)GetValue(ChildrenProperty); } set { SetValue(ChildrenProperty, value);}}
  21. public Color TextColor { get { return (Color)GetValue(TextColorProperty); } set { SetValue(TextColorProperty, value); } }
  22. public Color SelectedTextColor { get { return (Color)GetValue(SelectedTextColorProperty); } set { SetValue(SelectedTextColorProperty, value); } }
  23. public Color SelectedLineColor { get { return (Color)GetValue(SelectedLineColorProperty); } set { SetValue(SelectedLineColorProperty, value); } }
  24. public bool AutoScroll { get { return (bool)GetValue(AutoScrollProperty); } set { SetValue(AutoScrollProperty, value); } }
  25. StackLayout _mainContentLayout = new StackLayout(){ Spacing = 10, Orientation = StackOrientation.Horizontal };
  26. StackLayout _lastElementSelected;
  27. ScrollViewWithNotBar _mainLayout = new ScrollViewWithNotBar() {VerticalOptions = LayoutOptions.Start, Orientation = ScrollOrientation.Horizontal };
  28.  
  29. void LoadChildrens(){
  30. _mainContentLayout.Children.Clear();
  31. foreach (var item in Children)
  32. {
  33. var label = new Label()
  34. {
  35. FontAttributes= FontAttributes.Bold,
  36. Text = item,
  37. TextColor=TextColor,
  38. Margin= new Thickness(10,0),
  39. HorizontalOptions = LayoutOptions.CenterAndExpand
  40. };
  41.  
  42. var boxview = new BoxView() { BackgroundColor = Color.Transparent, HeightRequest = 2, HorizontalOptions = LayoutOptions.FillAndExpand };
  43. var childrenLayout = new StackLayout(){Spacing=5};
  44.  
  45. childrenLayout.Children.Add(label);
  46. childrenLayout.Children.Add(boxview);
  47. childrenLayout.ClassId = item;
  48. _mainContentLayout.Children.Add(childrenLayout);
  49. var tapGestureRecognizer = new TapGestureRecognizer();
  50. tapGestureRecognizer.Tapped += (s, e) => {
  51. ItemSelected = ((StackLayout)s).ClassId;
  52. };
  53. childrenLayout.GestureRecognizers.Add(tapGestureRecognizer);
  54. }
  55. _mainLayout.Content = _mainContentLayout;
  56. var mainContentLayout = new StackLayout() { Spacing = 0 };
  57. mainContentLayout.Children.Add(_mainLayout);
  58. mainContentLayout.Children.Add(new BoxView() { HeightRequest = 0.5, HorizontalOptions = LayoutOptions.FillAndExpand, BackgroundColor = Color.Silver });
  59.  
  60. this.Content = mainContentLayout;
  61.  
  62. }
  63.  
  64. void SelectElement(StackLayout itemSelected)
  65. {
  66. if (_lastElementSelected != null){
  67. (_lastElementSelected.Children.First(p => p is BoxView) as BoxView).BackgroundColor = Color.Transparent;
  68. (_lastElementSelected.Children.First(p => p is Label) as Label).TextColor = TextColor;
  69. }
  70.  
  71. (itemSelected.Children.First(p => p is BoxView) as BoxView).BackgroundColor = SelectedLineColor;
  72. (itemSelected.Children.First(p => p is Label) as Label).TextColor = SelectedTextColor;
  73. _lastElementSelected = itemSelected;
  74. if(AutoScroll)
  75. _mainLayout.ScrollToAsync(itemSelected, ScrollToPosition.MakeVisible, true);
  76. }
  77.  
  78. protected override void OnPropertyChanged(string propertyName = null)
  79. {
  80. base.OnPropertyChanged(propertyName);
  81.  
  82. if(propertyName == ItemSelectedProperty.PropertyName && Children != null && Children.Count > 0){
  83. SelectElement(_mainContentLayout.Children[Children.IndexOf(ItemSelected)] as StackLayout);
  84. } else if (propertyName == ChildrenProperty.PropertyName && Children != null)
  85. {
  86. LoadChildrens();
  87. if(string.IsNullOrEmpty(ItemSelected))
  88. {
  89. ItemSelected = Children.First();
  90. }else{
  91. SelectElement(_mainContentLayout.Children[Children.IndexOf(ItemSelected)] as StackLayout);
  92. }
  93. }
  94. }
  95. }
  96. }
Add Comment
Please, Sign In to add comment