Skip to content

Commit 0b5f4c5

Browse files
authored
Merge pull request #403 from iNKORE-NET/copilot/fix-collapsed-expander-height
Fix collapsed Expander flashing at full height on initial load
2 parents f00f1c6 + 3035da1 commit 0b5f4c5

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,7 @@ FodyWeavers.xsd
386386

387387
# JetBrains Rider
388388
.idea/
389-
*.sln.iml
389+
*.sln.iml
390+
391+
# NuGet directory
392+
.nuget/

source/iNKORE.UI.WPF.Modern/Controls/Helpers/ExpanderAnimationsHelper.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyCha
8989

9090
if (expander.IsLoaded)
9191
{
92-
RunExpanderAnimation(expander);
92+
InitializeExpanderState(expander);
9393
}
9494
else
9595
{
@@ -98,7 +98,7 @@ private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyCha
9898

9999
void TriggerExpandAnimationOnLoad(object sender, RoutedEventArgs routedEventArgs)
100100
{
101-
RunExpanderAnimation(expander);
101+
InitializeExpanderState(expander);
102102
expander.Loaded -= TriggerExpandAnimationOnLoad;
103103
}
104104
}
@@ -115,6 +115,51 @@ private static void OnExpanderExpandedOrCollapsed(object sender, RoutedEventArgs
115115

116116
#endregion
117117

118+
/// <summary>
119+
/// Initializes the visual state of an expander on initial load without animations.
120+
/// This method sets the expander content to the appropriate state (collapsed or expanded)
121+
/// immediately to prevent visual flashing during load.
122+
/// </summary>
123+
/// <remarks>
124+
/// This method is called during the initial load of the expander to avoid triggering
125+
/// layout updates and animations that would cause the content to briefly render at
126+
/// full size before collapsing. For collapsed expanders, it sets visibility directly
127+
/// to Collapsed. For expanded expanders, it ensures visibility is Visible and resets
128+
/// any transforms to identity without animation.
129+
/// </remarks>
130+
/// <seealso href="https://github.com/iNKORE-NET/UI.WPF.Modern/issues/402"/>
131+
private static void InitializeExpanderState(Expander expander)
132+
{
133+
// On initial load, set the content to the appropriate state immediately
134+
// without animation to avoid a visual flash
135+
var toAnimateControl = GetToAnimateControl(expander);
136+
if (toAnimateControl == null)
137+
{
138+
return;
139+
}
140+
141+
if (!expander.IsExpanded)
142+
{
143+
// If collapsed on load, set visibility to collapsed immediately
144+
toAnimateControl.Visibility = Visibility.Collapsed;
145+
}
146+
else
147+
{
148+
// If expanded on load, ensure visibility is set and clear any animations
149+
toAnimateControl.BeginAnimation(UIElement.VisibilityProperty, null);
150+
toAnimateControl.Visibility = Visibility.Visible;
151+
152+
// Clear any transform animations and reset transform to identity
153+
if (toAnimateControl.RenderTransform is TranslateTransform translateTransform)
154+
{
155+
translateTransform.BeginAnimation(TranslateTransform.XProperty, null);
156+
translateTransform.BeginAnimation(TranslateTransform.YProperty, null);
157+
translateTransform.X = 0;
158+
translateTransform.Y = 0;
159+
}
160+
}
161+
}
162+
118163
private static void RunExpanderAnimation(Expander expander)
119164
{
120165
if (expander.IsExpanded)

0 commit comments

Comments
 (0)