|
8 | 8 |
|
9 | 9 | namespace Accordion
|
10 | 10 | {
|
11 |
| - public class ExpendableView : ContentView |
| 11 | + public class ShoppingCart |
12 | 12 | {
|
13 |
| - private bool _isExpended; |
14 |
| - |
15 |
| - public ExpendableView(ScrollView parent) |
16 |
| - : this(async (view) => { await parent.ScrollToAsync(0, view.Y, true)<
8000
span class=pl-kos>; }) |
17 |
| - { } |
18 |
| - |
19 |
| - |
20 |
| - public ExpendableView(Action<ContentView> onSelected) |
21 |
| - { |
22 |
| - var header = new AbsoluteLayout { |
23 |
| - BackgroundColor = Color.FromHex("0067B7") |
24 |
| - }; |
25 |
| - var icon = |
26 |
| - new Image |
27 |
| - { |
28 |
| - Source = ImageSource.FromFile("ic_keyboard_arrow_right_white_24dp.png"), |
29 |
| - VerticalOptions = LayoutOptions.Center |
30 |
| - }; |
31 |
| - header.Children.Add(icon, new Rectangle(0, 1, .1, 1), AbsoluteLayoutFlags.All); |
32 |
| - header.Children.Add( |
33 |
| - new Label |
34 |
| - { |
35 |
| - Text = "October", |
36 |
| - TextColor = Color.White, |
37 |
| - BackgroundColor = Color.FromHex("0067B7"), |
38 |
| - VerticalTextAlignment = TextAlignment.Center, |
39 |
| - HeightRequest = 50 |
40 |
| - }, |
41 |
| - new Rectangle(1, 1, .9, 1), |
42 |
| - AbsoluteLayoutFlags.All); |
43 |
| - |
44 |
| - var list = |
45 |
| - new StackLayout |
46 |
| - { |
47 |
| - Children = { |
48 |
| - new Label { Text = "Hello", HeightRequest = 30 }, |
49 |
| - new Label { Text = "Hello", HeightRequest = 30 }, |
50 |
| - new Label { Text = "Hello", HeightRequest = 30 }, |
51 |
| - new Label { Text = "Hello", HeightRequest = 30 }, |
52 |
| - new Label { Text = "Hello", HeightRequest = 30 }, |
53 |
| - new Label { Text = "Hello", HeightRequest = 30 }, |
54 |
| - new Label { Text = "Hello", HeightRequest = 30 }, |
55 |
| - new Label { Text = "Hello", HeightRequest = 30 }, |
56 |
| - new Label { Text = "Hello", HeightRequest = 30 } |
57 |
| - }, |
58 |
| - HeightRequest = 0, |
59 |
| - |
60 |
| - }; |
61 |
| - |
62 |
| - var layout = |
63 |
| - new StackLayout |
64 |
| - { |
65 |
| - Spacing = 0, |
66 |
| - Children = { |
67 |
| - header, |
68 |
| - list |
69 |
| - } |
70 |
| - }; |
| 13 | + public DateTime Date { get; set; } |
| 14 | + public double Amount { get; set; } |
| 15 | + } |
71 | 16 |
|
72 |
| - header.GestureRecognizers.Add( |
73 |
| - new TapGestureRecognizer |
74 |
| - { |
75 |
| - Command = new Command(() => |
76 |
| - { |
77 |
| - if (_isExpended) |
78 |
| - { |
79 |
| - list.HeightRequest = 0; |
80 |
| - icon.Source = ImageSource.FromFile("ic_keyboard_arrow_right_white_24dp.png"); |
81 |
| - _isExpended = false; |
82 |
| - } |
83 |
| - else |
84 |
| - { |
85 |
| - list.HeightRequest = list.Children.Count * 30; |
86 |
| - icon.Source = ImageSource.FromFile("ic_keyboard_arrow_down_white_24dp.png"); |
87 |
| - onSelected(this); |
88 |
| - _isExpended = true; |
89 |
| - } |
90 |
| - }) |
91 |
| - } |
92 |
| - ); |
| 17 | + public class Section |
| 18 | + { |
| 19 | + public string Title { get; set; } |
| 20 | + public IEnumerable<ShoppingCart> List { get; set; } |
| 21 | + } |
93 | 22 |
|
94 |
| - this.Content = layout; |
95 |
| - } |
| 23 | + public class ViewModel |
| 24 | + { |
| 25 | + public IEnumerable<Section> List { get; set; } |
96 | 26 | }
|
97 | 27 |
|
98 | 28 | public class AccordionViewPage : ContentPage
|
99 | 29 | {
|
100 | 30 | public AccordionViewPage()
|
101 | 31 | {
|
102 |
| - var scrollView = new ScrollView(); |
| 32 | + this.Title = "Accordion"; |
103 | 33 |
|
104 |
| - var layout = |
105 |
| - new StackLayout |
106 |
| - { |
107 |
| - Spacing = 1, |
108 |
| - Children = { |
109 |
| - new ExpendableView(scrollView), |
110 |
| - new ExpendableView(scrollView), |
111 |
| - new ExpendableView(scrollView), |
112 |
| - new ExpendableView(scrollView), |
113 |
| - new ExpendableView(scrollView), |
114 |
| - new ExpendableView(scrollView), |
115 |
| - new ExpendableView(scrollView), |
116 |
| - new ExpendableView(scrollView), |
117 |
| - new ExpendableView(scrollView), |
118 |
| - new ExpendableView(scrollView), |
119 |
| - new ExpendableView(scrollView), |
120 |
| - new ExpendableView(scrollView), |
121 |
| - new ExpendableView(scrollView) |
122 |
| - } |
123 |
| - }; |
| 34 | + var template = new DataTemplate(() => |
| 35 | + { |
| 36 | + var layout = new AbsoluteLayout { Padding = 5 }; |
| 37 | + var title = new Label { HorizontalTextAlignment = TextAlignment.Start }; |
| 38 | + var price = new Label { HorizontalTextAlignment = TextAlignment.End }; |
| 39 | + layout.Children.Add(title, new Rectangle(0, 1, 0.5, 1), AbsoluteLayoutFlags.All); |
| 40 | + layout.Children.Add(price, new Rectangle(0, 1, 0.5, 1), AbsoluteLayoutFlags.All); |
| 41 | + title.SetBinding(Label.TextProperty, "Date", stringFormat: "{0:dd MMM yyyy}"); |
| 42 | + price.SetBinding(Label.TextProperty, "Amount", stringFormat: "{0:C2}"); |
| 43 | + return (object)layout; |
| 44 | + }); |
124 | 45 |
|
125 |
| - scrollView.Content = layout; |
| 46 | + var view = new AccordionView(template); |
| 47 | + view.SetBinding(AccordionView.ItemsSourceProperty, "List"); |
| 48 | + view.Template.SetBinding(AccordionSectionView.TitleProperty, "Title"); |
| 49 | + view.Template.SetBinding(AccordionSectionView.ItemsSourceProperty, "List"); |
126 | 50 |
|
127 |
| - this.Title = "Accordion"; |
128 |
| - this.Content = scrollView; |
| 51 | + view.BindingContext = |
| 52 | + new ViewModel |
| 53 | + { |
| 54 | + List = new List<Section> { |
| 55 | + new Section |
| 56 | + { |
| 57 | + Title = "October", |
| 58 | + List = new List<ShoppingCart> { |
| 59 | + new ShoppingCart { Date = DateTime.UtcNow, Amount = 10.05 } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + }; |
| 64 | + this.Content = view; |
129 | 65 | }
|
130 | 66 | }
|
131 | 67 |
|
132 | 68 | public class App : Application
|
133 | 69 | {
|
134 | 70 | public App()
|
135 | 71 | {
|
136 |
| - var page = |
| 72 | + var page = |
137 | 73 | new TabbedPage
|
138 | 74 | {
|
139 |
| - Children = { |
| 75 | + Children = { |
140 | 76 | new AccordionViewPage(),
|
141 | 77 | new CustomViewTestPage()
|
142 | 78 | }
|
|
0 commit comments