{"id":417,"date":"2022-12-03T20:14:01","date_gmt":"2022-12-03T20:14:01","guid":{"rendered":"https:\/\/learnmobiledevelopment.com\/?p=417"},"modified":"2023-01-18T16:47:54","modified_gmt":"2023-01-18T16:47:54","slug":"expander-in-maui","status":"publish","type":"post","link":"https:\/\/learnmobiledevelopment.com\/index.php\/2022\/12\/03\/expander-in-maui\/","title":{"rendered":"How to use Expander in MAUI"},"content":{"rendered":"\n<p>The Xamarin.CommunityToolkit <strong>Expander<\/strong> <strong>in<strong> MAUI<\/strong><\/strong> view provides an expandable container to host any content.&nbsp;It has two main properties to store your content that is <strong>Header<\/strong> &amp; <strong>Content<\/strong>. User can show or hide its content by interacting with the <strong>header<\/strong> of the control.<\/p>\n\n\n\n<p>In this article, I\u2019m going to show you how to use&nbsp;<strong><strong>Expander <\/strong>in<strong> MAUI<\/strong><\/strong> <strong>Community Toolkit<\/strong>. <\/p>\n\n\n\n<p><strong>Expander<\/strong> has &#8220;<strong>Direction<\/strong>&#8221; property which defines the expander direction.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&nbsp;<strong>ExpandDirection<\/strong><br><strong>&#8220;Down&#8221;<\/strong> =&gt; It indicates that the <strong>Expander<\/strong> content is under the header.<br><strong>&#8220;Up&#8221;<\/strong>  =&gt; It indicates that the <strong>Expander<\/strong> content is above the header.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>&nbsp;<\/strong><em><strong>Note : Expander<\/strong> is not supported inside <strong>CollectionView<\/strong> or <strong>ListView<\/strong>. May be in future version of <strong>.NET MAUI Community Toolkit<\/strong> will support.<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p style=\"font-size:25px\"><strong>Let\u2019s Start<\/strong><\/p>\n\n\n\n<p>In this sample demo we will use&nbsp;<strong><a href=\"https:\/\/www.nuget.org\/packages\/CommunityToolkit.Maui\/\" target=\"_blank\" rel=\"noreferrer noopener\">CommunityToolkit.Maui<\/a><\/strong>&nbsp;nuget package.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>CommunityToolkit.Maui<\/strong><\/p>\n\n\n\n<p>CommunityToolkit.Maui&nbsp;is a collection of Converters, Behaviours, Animations, Custom Views, Effects, Helpers etc. It simplifies the developers task when building Android, iOS, macOS and WinUI applications using MAUI.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>1 &#8211;<strong> Install CommunityToolkit.Maui&nbsp;nuget package<\/strong><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"606\" src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-1024x606.png\" alt=\"Expander in MAUI\" class=\"wp-image-422\" srcset=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-1024x606.png 1024w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-300x178.png 300w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-768x455.png 768w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-1536x909.png 1536w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-850x503.png 850w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529.png 1802w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>2 &#8211; In order to use the .NET MAUI Community Toolkit you need to call the extension method in your MauiProgram.cs&nbsp;file as follows<\/strong> :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using CommunityToolkit.Maui;\nusing Microsoft.Extensions.Logging;\n\nnamespace MAUIExpanderViewSample;\n\npublic static class MauiProgram\n{\n\tpublic static MauiApp CreateMauiApp()\n\t{\n\t\tvar builder = MauiApp.CreateBuilder();\n\t\tbuilder\n\t\t\t.UseMauiApp&lt;App>()\n\t\t\t.UseMauiCommunityToolkit()\n\t\t\t.ConfigureFonts(fonts =>\n\t\t\t{\n\t\t\t\tfonts.AddFont(\"OpenSans-Regular.ttf\", \"OpenSansRegular\");\n\t\t\t\tfonts.AddFont(\"OpenSans-Semibold.ttf\", \"OpenSansSemibold\");\n\t\t\t});\n\n#if DEBUG\n\t\tbuilder.Logging.AddDebug();\n#endif\n\n\t\treturn builder.Build();\n\t}\n}<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>3 &#8211; Setting up the UI<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>3.1 &#8211; MainPage.xaml<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?>\n&lt;ContentPage xmlns=\"http:\/\/schemas.microsoft.com\/dotnet\/2021\/maui\"\n             xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\"\n             xmlns:toolkit=\"http:\/\/schemas.microsoft.com\/dotnet\/2022\/maui\/toolkit\"\n             x:Class=\"MAUIExpanderViewSample.MainPage\">\n\n    &lt;ScrollView Padding=\"10\"\n                VerticalScrollBarVisibility=\"Always\"\n                VerticalOptions=\"FillAndExpand\">\n        &lt;StackLayout\n            Spacing=\"25\"\n            Padding=\"10,0\"\n            VerticalOptions=\"Start\">\n\n            &lt;!--Expander in Down Direction-->\n            &lt;Border Stroke=\"#2B0B98\"\n                    StrokeThickness=\"4\"\n                    StrokeShape=\"RoundRectangle 40,0,0,40\"\n                    Background=\"#fff\"\n                    Padding=\"16,8\"\n                    HorizontalOptions=\"FillAndExpand\">\n\n                &lt;toolkit:Expander Direction=\"Down\">\n                    &lt;toolkit:Expander.Header>\n                          &lt;Button Text=\"Expand in Down Direction !!\"  HorizontalOptions=\"CenterAndExpand\"\/>\n                    &lt;\/toolkit:Expander.Header>\n\n                    &lt;toolkit:Expander.Content>\n                        &lt;VerticalStackLayout Spacing=\"20\" Padding=\"10\">\n                            &lt;Image\n                                Source=\"dotnet_bot.png\"\n                                SemanticProperties.Description=\"Cute dot net bot waving hi to you!\"\n                                HeightRequest=\"200\"\n                                HorizontalOptions=\"Center\" \/>\n\n                            &lt;Label\n                                Text=\"Hello, World!\"\n                                SemanticProperties.HeadingLevel=\"Level1\"\n                                FontSize=\"32\"\n                                HorizontalOptions=\"Center\" \/>\n\n                            &lt;Label\n                                Text=\"Welcome to .NET Multi-platform App UI\"\n                                SemanticProperties.HeadingLevel=\"Level2\"\n                                SemanticProperties.Description=\"Welcome to dot net Multi platform App U I\"\n                                FontSize=\"18\"\n                                HorizontalOptions=\"Center\" \/>\n\n                            &lt;Button\n                            x:Name=\"CounterBtn\"\n                            Text=\"Click me\"\n                            SemanticProperties.Hint=\"Counts the number of times you click\"\n                            Clicked=\"OnCounterClicked\"\n                            HorizontalOptions=\"Center\" \/>\n                        &lt;\/VerticalStackLayout>\n                    &lt;\/toolkit:Expander.Content>\n\n               &lt;\/toolkit:Expander>\n            &lt;\/Border>\n\n            &lt;!--Expander in Up Direction-->\n            &lt;Border Stroke=\"#2B0B98\"\n                    StrokeThickness=\"4\"\n                    StrokeShape=\"RoundRectangle 40,0,0,40\"\n                    Background=\"#fff\"\n                    Padding=\"16,8\"\n                    HorizontalOptions=\"FillAndExpand\">\n\n                &lt;toolkit:Expander Direction=\"Up\">\n                    &lt;toolkit:Expander.Header>\n                          &lt;Button Text=\"Expand in Up Direction !!\" HorizontalOptions=\"CenterAndExpand\"\/>\n                    &lt;\/toolkit:Expander.Header>\n\n                    &lt;toolkit:Expander.Content>\n                        &lt;VerticalStackLayout Spacing=\"20\" Padding=\"10\">\n                            &lt;Image\n                                Source=\"dotnet_bot.png\"\n                                SemanticProperties.Description=\"Cute dot net bot waving hi to you!\"\n                                HeightRequest=\"200\"\n                                HorizontalOptions=\"Center\" \/>\n\n                            &lt;Label\n                                Text=\"Hello, World!\"\n                                SemanticProperties.HeadingLevel=\"Level1\"\n                                FontSize=\"32\"\n                                HorizontalOptions=\"Center\" \/>\n\n                            &lt;Label\n                                Text=\"Welcome to .NET Multi-platform App UI\"\n                                SemanticProperties.HeadingLevel=\"Level2\"\n                                SemanticProperties.Description=\"Welcome to dot net Multi platform App U I\"\n                                FontSize=\"18\"\n                                HorizontalOptions=\"Center\" \/>\n\n                            &lt;Button\n                            Text=\"Click me\"\n                            SemanticProperties.Hint=\"Counts the number of times you click\"\n                            Clicked=\"OnCounterClicked\"\n                            HorizontalOptions=\"Center\" \/>\n                        &lt;\/VerticalStackLayout>\n                    &lt;\/toolkit:Expander.Content>\n\n               &lt;\/toolkit:Expander>\n            &lt;\/Border>\n\n            &lt;!--Nested Expander in Down Direction-->\n            &lt;Border Stroke=\"#2B0B98\"\n                    StrokeThickness=\"4\"\n                    StrokeShape=\"RoundRectangle 40,0,0,40\"\n                    Background=\"#fff\"\n                    Padding=\"16,8\"\n                    HorizontalOptions=\"FillAndExpand\">\n\n                &lt;toolkit:Expander Direction=\"Down\">\n                    &lt;toolkit:Expander.Header>\n                          &lt;Button Text=\"Nested Expand in Down Direction !!\" HorizontalOptions=\"CenterAndExpand\"\/>\n                    &lt;\/toolkit:Expander.Header>\n\n                    &lt;toolkit:Expander.Content>\n                        &lt;VerticalStackLayout Spacing=\"20\" Padding=\"10\">\n\n                            &lt;toolkit:Expander Direction=\"Down\">\n                                &lt;toolkit:Expander.Header>\n                                    &lt;Image\n                                        Source=\"dotnet_bot.png\"\n                                        SemanticProperties.Description=\"Cute dot net bot waving hi to you!\"\n                                        HeightRequest=\"200\"\n                                        HorizontalOptions=\"Center\" \/>\n                               &lt;\/toolkit:Expander.Header>\n                                &lt;toolkit:Expander.Content>\n                                    &lt;toolkit:Expander Direction=\"Down\">\n                                        &lt;toolkit:Expander.Header>\n                                            &lt;Label\n                                                Text=\"Hello, World!\"\n                                                SemanticProperties.HeadingLevel=\"Level1\"\n                                                FontSize=\"32\"\n                                                HorizontalOptions=\"Center\" \/>\n                                        &lt;\/toolkit:Expander.Header>\n                                        &lt;toolkit:Expander.Content>\n                                            &lt;toolkit:Expander Direction=\"Down\">\n                                                &lt;toolkit:Expander.Header>\n                                                    &lt;Label\n                                                        Text=\"Welcome to .NET Multi-platform App UI\"\n                                                        SemanticProperties.HeadingLevel=\"Level2\"\n                                                        SemanticProperties.Description=\"Welcome to dot net Multi platform App U I\"\n                                                        FontSize=\"18\"\n                                                        HorizontalOptions=\"Center\" \/>\n                                                &lt;\/toolkit:Expander.Header>\n                                                &lt;toolkit:Expander.Content>\n                                                    &lt;Button\n                                                        Text=\"Click me\"\n                                                        SemanticProperties.Hint=\"Counts the number of times you click\"\n                                                        Clicked=\"OnCounterClicked\"\n                                                        HorizontalOptions=\"Center\" \/>\n                                                &lt;\/toolkit:Expander.Content>\n                                            &lt;\/toolkit:Expander>\n                                        &lt;\/toolkit:Expander.Content>\n                                        \n                                    &lt;\/toolkit:Expander>\n                                &lt;\/toolkit:Expander.Content>\n                            &lt;\/toolkit:Expander> \n                        &lt;\/VerticalStackLayout>\n                    &lt;\/toolkit:Expander.Content>\n\n               &lt;\/toolkit:Expander>\n            &lt;\/Border>\n            \n        &lt;\/StackLayout>\n    &lt;\/ScrollView>\n\n&lt;\/ContentPage><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>3.2 &#8211; MainPage.xaml.cs<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">namespace MAUIExpanderViewSample;\n\npublic partial class MainPage : ContentPage\n{\n\tint count = 0;\n\n\tpublic MainPage()\n\t{\n\t\tInitializeComponent();\n\t}\n\n\tprivate void OnCounterClicked(object sender, EventArgs e)\n\t{\n\t\tcount++;\n\n\t\tif (count == 1)\n\t\t\tCounterBtn.Text = $\"Clicked {count} time\";\n\t\telse\n\t\t\tCounterBtn.Text = $\"Clicked {count} times\";\n\n\t\tSemanticScreenReader.Announce(CounterBtn.Text);\n\t}\n}<\/pre>\n\n\n\n<p>Now, We have completed the coding, Let&#8217;s try to run the application.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>4 &#8211; Result<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1564\" style=\"aspect-ratio: 732 \/ 1564;\" width=\"732\" controls muted src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderInMAUI-1.mov\"><\/video><\/figure>\n\n\n\n<p>That\u2019s all for now!<\/p>\n\n\n\n<p>You can check the full source code <strong><a href=\"https:\/\/github.com\/Alam-Ashraf\/MAUIExpanderViewSample\" target=\"_blank\" rel=\"noopener\" title=\"\">here<\/a>.<\/strong><\/p>\n\n\n\n<p>Happy Coding!<strong> \ud83d\ude00<\/strong><\/p>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-medium-font-size\"><strong>You may also like<\/strong><\/p>\n\n\n\n<div class=\"wp-block-group is-content-justification-space-between is-nowrap is-layout-flex wp-container-core-group-is-layout-0dfbf163 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\"><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/learnmobiledevelopment.com\/index.php\/2022\/12\/07\/docklayout-in-maui\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/DockLayoutInMAUI-1024x589.png\" alt=\"DockLayout in MAUI\" class=\"wp-image-445\" width=\"512\" height=\"295\" srcset=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/DockLayoutInMAUI-1024x589.png 1024w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/DockLayoutInMAUI-300x172.png 300w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/DockLayoutInMAUI-768x442.png 768w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/DockLayoutInMAUI-850x489.png 850w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/DockLayoutInMAUI.png 1294w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\">How to use DockLayout in MAUI<\/p>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\"><div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/learnmobiledevelopment.com\/index.php\/2023\/01\/10\/observableobject-in-maui-for-data-binding\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/ObservableObject-in-MAUI-1024x733.png\" alt=\"ObservableObject in MAUI\" class=\"wp-image-686\" width=\"512\" height=\"367\" srcset=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/ObservableObject-in-MAUI-1024x733.png 1024w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/ObservableObject-in-MAUI-300x215.png 300w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/ObservableObject-in-MAUI-768x550.png 768w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/ObservableObject-in-MAUI-850x609.png 850w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/ObservableObject-in-MAUI.png 1486w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\">How to use ObservableObject in MAUI for Data Binding<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Xamarin.CommunityToolkit Expander in MAUI view provides an expandable container to host any content.&nbsp;It has two main properties to store your content that is Header &amp; Content. User can show or hide its content by interacting with the header of the control. In this article, I\u2019m going to show you how to use&nbsp;Expander in MAUI&#8230;<\/p>\n","protected":false},"author":1,"featured_media":435,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[28,27,30,29,34],"class_list":["post-417","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnetmaui","tag-expander-in-net-maui","tag-expander-in-dot-net-maui","tag-expander-view-net-maui-community-toolkit","tag-expander-view-in-net-maui","tag-xamarin-communitytoolkit"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/comments?post=417"}],"version-history":[{"count":25,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":754,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/417\/revisions\/754"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/media\/435"}],"wp:attachment":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/media?parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}