Expander animation #1628
Replies: 5 comments 5 replies
-
Can you give a bit more information on what you are looking for here please. |
Beta Was this translation helpful? Give feedback.
-
Sure, I have just updated my question with more specific information. |
Beta Was this translation helpful? Give feedback.
-
@BeepBeepBopBop would you be able to supply a referenced video (e.g. YouTube or animated gif), so we can visualize what you're after? |
Beta Was this translation helpful? Give feedback.
-
You can implement this by hooking up animations to property change events on Expander.IsExpanded. In the following example:
When Expanded.IsExpanded=true
When Expanded.IsExpanded=false I do the converse. <!-- ExpandPage.xaml -->
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="Maui.StackOverflow.ExpanderPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Name="thisPage"
Title="ExpanderPage">
<VerticalStackLayout Padding="20,20,20,20" Spacing="20">
<toolkit:Expander x:Name="expander">
<toolkit:Expander.Header>
<Grid ColumnDefinitions="*,Auto">
<Label Text="What is C# used for?" />
<Label x:Name="caret" Grid.Column="1" Text="^" />
</Grid>
</toolkit:Expander.Header>
<ScrollView x:Name="contentOuter">
<VerticalStackLayout x:Name="contentInner" Padding="0,20,0,20" Spacing="20">
<Label Text="Some of the most important C# uses that enterprises benefit from include web app development, game development, workflow applications, and Windows services." />
<Label Text="TestGorilla" />
<Label Text="What is C# used for and which companies used it? - TestGorilla" />
</VerticalStackLayout>
</ScrollView>
</toolkit:Expander>
<Label Text="Is C# easier than C++?" />
<Label Text="Is it worth learning C# in 2024?" />
<Label Text="Is C# difficult to learn?" />
<Label Text="Is C# hard after Python?" />
</VerticalStackLayout>
</ContentPage> // ExpandPage.xaml.cs
using CommunityToolkit.Maui.Views;
namespace Maui.StackOverflow;
public partial class ExpanderPage : ContentPage
{
public ExpanderPage()
{
InitializeComponent();
expander.PropertyChanged += (s, e) =>
{
switch (e.PropertyName)
{
case nameof(Expander.IsExpanded):
if (expander.IsExpanded)
{
caret.RotateTo(180);
var animation = new Animation(v => contentOuter.HeightRequest = v, 0, contentInner.Height);
animation.Commit(this, "ExpandContent");
}
else
{
caret.RotateTo(0);
var animation = new Animation(v => contentOuter.HeightRequest = v, contentInner.Height, 0);
animation.Commit(this, "CollapseContent");
}
break;
}
};
}
} |
Beta Was this translation helpful? Give feedback.
-
@IeuanWalker I've updated the example to support dynamic content height |
Beta Was this translation helpful? Give feedback.
-
It would be nice to be able to animate the Expander control upon expanding
More specifically:
For now the API includes a IsExpanded flag whose value changes whether only the header or the header + the content of the expander is shown.
It would be nice to add a way to add a visual animation for the transition between the "expanded" and "not expanded" state change induced by the toggling of IsExpanded property. For example adding another boolean property ShouldAnimateExpandedChanges or a method like ToggleIsExpanded providing a shouldAnimate parameter.
Beta Was this translation helpful? Give feedback.
All reactions