|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: BindableLayout in MAUI Cards control | Syncfusion<sup>®</sup> |
| 4 | +description: Learn about BindableLayout support in Syncfusion<sup>®</sup> Essential Studio<sup>®</sup> MAUI Cards control, its elements and more. |
| 5 | +platform: MAUI |
| 6 | +control: Cards |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# BindableLayout in MAUI Cards |
| 11 | + |
| 12 | +Layout<T> introduces a feature called `BindableLayout`, which works with all layouts derived from Layout<T>. By simply setting the ItemTemplate and ItemsSource, BindableLayout automatically generates a group of UI elements (based on the provided ItemTemplate) for each data item in the ItemsSource and adds them as children. |
| 13 | + |
| 14 | +Since [`SfCardLayout`](https://help.syncfusion.com/cr/maui-toolkit/Syncfusion.Maui.Toolkit.Cards.SfCardLayout.html) is an extended class of Layout<T>, this approach is also possible for `SfCardLayout`. |
| 15 | + |
| 16 | +## Initialize view model |
| 17 | + |
| 18 | +Define a simple data model that represents data to be populated for [`SfCardLayout`](https://help.syncfusion.com/cr/maui-toolkit/Syncfusion.Maui.Toolkit.Cards.SfCardLayout.html). |
| 19 | + |
| 20 | +{% highlight c# %} |
| 21 | + |
| 22 | +public class Model |
| 23 | +{ |
| 24 | + public IEnumerable<string> Colors { get; set; } |
| 25 | +} |
| 26 | + |
| 27 | +{% endhighlight %} |
| 28 | + |
| 29 | +Next, create a view model class and initialize a model object as demonstrated in the following code sample. |
| 30 | + |
| 31 | +{% highlight c# %} |
| 32 | + |
| 33 | +public class ViewModel |
| 34 | +{ |
| 35 | + public Model Model { get; set; } |
| 36 | + |
| 37 | + public ViewModel() |
| 38 | + { |
| 39 | + Model = new Model |
| 40 | + { |
| 41 | + Colors = new string[] |
| 42 | + { |
| 43 | + "Cyan", "Yellow", "Orange" |
| 44 | + } |
| 45 | + }; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | +{% endhighlight %} |
| 50 | + |
| 51 | +Set the ViewModel instance as BindingContext of your page to bind properties of ViewModel to [`SfCardLayout`](https://help.syncfusion.com/cr/maui-toolkit/Syncfusion.Maui.Toolkit.Cards.SfCardLayout.html). |
| 52 | + |
| 53 | +N> Add namespace of ViewModel class in your XAML page if you prefer to set BindingContext in XAML. |
| 54 | + |
| 55 | +{% tabs %} |
| 56 | + |
| 57 | +{% highlight xaml %} |
| 58 | + |
| 59 | +<ContentPage.BindingContext> |
| 60 | + |
| 61 | +<local:ViewModel/> |
| 62 | + |
| 63 | +</ContentPage.BindingContext> |
| 64 | + |
| 65 | +{% endhighlight %} |
| 66 | + |
| 67 | +{% highlight c# %} |
| 68 | + |
| 69 | +this.BindingContext = new ViewModel(); |
| 70 | + |
| 71 | +{% endhighlight %} |
| 72 | + |
| 73 | +{% endtabs %} |
| 74 | + |
| 75 | +## Populate CardLayout with data |
| 76 | + |
| 77 | +[`SfCardLayout`](https://help.syncfusion.com/cr/maui-toolkit/Syncfusion.Maui.Toolkit.Cards.SfCardLayout.html) can be populated with data by setting the ItemSource property of BindableLayout to a collection of items that can be used in [`SfCardView`](https://help.syncfusion.com/cr/maui-toolkit/Syncfusion.Maui.Toolkit.Cards.SfCardView.html). |
| 78 | + |
| 79 | +{% tabs %} |
| 80 | + |
| 81 | +{% highlight xaml %} |
| 82 | + |
| 83 | +<cards:SfCardLayout BindableLayout.ItemsSource="{Binding Model.Colors}"> |
| 84 | +… |
| 85 | +</cards:SfCardLayout> |
| 86 | + |
| 87 | +{% endhighlight %} |
| 88 | + |
| 89 | +{% highlight c# %} |
| 90 | + |
| 91 | +SfCardLayout cardLayout = new SfCardLayout(); |
| 92 | +BindableLayout.SetItemsSource(cardLayout, viewModel.Model.Colors); |
| 93 | + |
| 94 | +{% endhighlight %} |
| 95 | + |
| 96 | +{% endtabs %} |
| 97 | + |
| 98 | +## Define the appearance of SfCardView |
| 99 | + |
| 100 | +[`SfCardLayout`](https://help.syncfusion.com/cr/maui-toolkit/Syncfusion.Maui.Toolkit.Cards.SfCardLayout.html) accepts only [`SfCardView`](https://help.syncfusion.com/cr/maui-toolkit/Syncfusion.Maui.Toolkit.Cards.SfCardView.html) as its child. The appearance of each [`SfCardView`](https://help.syncfusion.com/cr/maui-toolkit/Syncfusion.Maui.Toolkit.Cards.SfCardView.html) can be defined by setting the `BindableLayout.ItemTemplate` property. |
| 101 | + |
| 102 | +{% tabs %} |
| 103 | + |
| 104 | +{% highlight xaml %} |
| 105 | + |
| 106 | +<cards:SfCardLayout BindableLayout.ItemsSource="{Binding Model.Colors}" SwipeDirection="Left" VerticalOptions="Center" HeightRequest="300" WidthRequest="300" BackgroundColor="#F0F0F0"> |
| 107 | + <BindableLayout.ItemTemplate> |
| 108 | + <DataTemplate> |
| 109 | + <cards:SfCardView BackgroundColor="{Binding}"> |
| 110 | + <Label Text="{Binding}" HorizontalOptions="CenterAndExpand" VerticalTextAlignment="Center"/> |
| 111 | + </cards:SfCardView> |
| 112 | + </DataTemplate> |
| 113 | + </BindableLayout.ItemTemplate> |
| 114 | +</cards:SfCardLayout> |
| 115 | + |
| 116 | +{% endhighlight %} |
| 117 | + |
| 118 | +{% highlight c# %} |
| 119 | + |
| 120 | +SfCardLayout cardLayout = new SfCardLayout() |
| 121 | +{ |
| 122 | + SwipeDirection = CardSwipeDirection.Left, |
| 123 | + BackgroundColor = Color.FromHex("#F0F0F0"), |
| 124 | + VerticalOptions = LayoutOptions.Center, |
| 125 | + HeightRequest = 300, |
| 126 | + WidthRequest = 300, |
| 127 | +}; |
| 128 | + |
| 129 | +this.BindingContext = viewModel; |
| 130 | + |
| 131 | +DataTemplate dataTemplate = new DataTemplate(() => |
| 132 | +{ |
| 133 | + SfCardView cardView = new SfCardView(); |
| 134 | + cardView.SetBinding(SfCardView.BackgroundColorProperty, new Binding() { Path = "."}); |
| 135 | + |
| 136 | + Label label = new Label() |
| 137 | + { |
| 138 | + HorizontalOptions = LayoutOptions.CenterAndExpand, |
| 139 | + VerticalTextAlignment = TextAlignment.Center |
| 140 | + }; |
| 141 | + |
| 142 | + label.SetBinding(Label.TextProperty, new Binding() { Path= "."}); |
| 143 | + cardView.Content = label; |
| 144 | + return cardView; |
| 145 | +}); |
| 146 | + |
| 147 | +BindableLayout.SetItemTemplate(cardLayout, dataTemplate); |
| 148 | +BindableLayout.SetItemsSource(cardLayout, viewModel.Model.Colors); |
| 149 | + |
| 150 | +this.Content = cardLayout; |
| 151 | + |
| 152 | +{% endhighlight %} |
| 153 | + |
| 154 | +{% endtabs %} |
0 commit comments