-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCfCollectionMultiSelectionPopup.xaml.cs
More file actions
134 lines (113 loc) · 4.88 KB
/
Copy pathCfCollectionMultiSelectionPopup.xaml.cs
File metadata and controls
134 lines (113 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System.Collections;
using CraftUI.Library.Maui.Common.Helpers;
using CraftUI.Library.Maui.Converters;
using CraftUI.Library.Maui.MarkupExtensions;
namespace CraftUI.Library.Maui.Controls.Popups;
public partial class CfCollectionMultiSelectionPopup
{
public static readonly BindableProperty TitleProperty = BindableProperty.Create(nameof(Title), typeof(string), typeof(CfCollectionMultiSelectionPopup), propertyChanged: TitleChanged, defaultBindingMode: BindingMode.OneWayToSource);
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IList), typeof(CfCollectionMultiSelectionPopup), propertyChanged: ItemsSourceChanged);
public static readonly BindableProperty SelectedItemsProperty = BindableProperty.Create(nameof(SelectedItems), typeof(IList<object>), typeof(CfCollectionMultiSelectionPopup), defaultBindingMode: BindingMode.TwoWay);
public static readonly BindableProperty ItemDisplayProperty = BindableProperty.Create(nameof(ItemDisplay), typeof(string), typeof(CfCollectionMultiSelectionPopup), defaultBindingMode: BindingMode.OneWayToSource);
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public IList? ItemsSource
{
get => (IList?)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public IList<object>? SelectedItems
{
get => (IList<object>?)GetValue(SelectedItemsProperty);
set => SetValue(SelectedItemsProperty, value);
}
public string? ItemDisplay
{
get => (string?)GetValue(ItemDisplayProperty);
set
{
SetValue(ItemDisplayProperty, value);
InitCollectionViewItems();
}
}
public CfCollectionMultiSelectionPopup()
{
InitializeComponent();
var tapped = new TapGestureRecognizer();
tapped.Tapped += (_, _) => Close();
CloseImage.GestureRecognizers.Add(tapped);
}
private static void TitleChanged(BindableObject bindable, object oldValue, object newValue) => ((CfCollectionMultiSelectionPopup)bindable).UpdateTitleView();
private static void ItemsSourceChanged(BindableObject bindable, object oldValue, object newValue) => ((CfCollectionMultiSelectionPopup)bindable).UpdateItemsSourceView();
private void UpdateTitleView() => TitleLabel.Text = Title;
private void UpdateItemsSourceView()
{
PickerCollectionView.ItemsSource = ItemsSource;
}
private void InitCollectionViewItems()
{
PickerCollectionView.ItemTemplate = new DataTemplate(() =>
{
var contentView = new ContentView
{
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.Fill,
Padding = new Thickness(16, 14),
WidthRequest = GutterSystem.WidthScreen,
BackgroundColor = Colors.Transparent
};
contentView.Triggers.Add(new DataTrigger(typeof(ContentView))
{
Binding = new Binding
{
Path = ".",
Converter = new SelectedItemsContainsConverter(),
ConverterParameter = this
},
Value = true,
Setters =
{
new Setter
{
Property = VisualElement.BackgroundColorProperty,
Value = ResourceHelper.GetThemeColor("PrimaryDark", "Primary")
}
}
});
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, _) =>
{
var item = ((ContentView)s!).BindingContext;
if (SelectedItems is null)
{
return;
}
if (SelectedItems.Contains(item))
{
SelectedItems.Remove(item);
}
else
{
SelectedItems.Add(item);
}
// Force only the tapped item to update its bindings
((ContentView)s).BindingContext = null;
((ContentView)s).BindingContext = item;
};
contentView.GestureRecognizers.Add(tapGestureRecognizer);
var label = new Label
{
FontSize = 16,
HorizontalOptions = LayoutOptions.Fill,
LineBreakMode = LineBreakMode.TailTruncation
};
label.SetBinding(Label.TextProperty, new Binding(ItemDisplay));
contentView.Content = label;
return contentView;
});
PickerCollectionView.MaximumHeightRequest = GutterSystem.HeightScreen / 1.3;
}
}