Skip to content

Commit

Permalink
Modified Expander animation mechanism in Fluent to avoid Argument Nul…
Browse files Browse the repository at this point in the history
…l Exception (#10130)

* Modified Expander animation mechanism in Fluent to avoid NRE exception

* Addressing PR review comments
  • Loading branch information
dipeshmsft authored Dec 9, 2024
1 parent 090a523 commit 53a7636
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur
return 0.0;
}

if (values[1] is not double factor)
if (values[1] is not double factor || factor == double.NaN)
{
return 0.0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@
</Border>

<Grid x:Name="ContentPresenterGrid" DockPanel.Dock="Bottom" ClipToBounds="True">
<!-- Dummy border to store Animation factor for expander -->
<Border x:Name="AnimationFactorBorder" Width="0" Visibility="Collapsed" />
<Border x:Name="ContentPresenterBorder"
Background="{DynamicResource ExpanderContentBackground}"
BorderBrush="{TemplateBinding BorderBrush}"
Expand All @@ -327,23 +329,36 @@
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}" />
<Border.Tag>
<system:Double>0.0</system:Double>
</Border.Tag>
<Border.Resources>
<TranslateTransform x:Key="HeightAnimation">
<TranslateTransform x:Key="HeightAnimationNegative">
<TranslateTransform.Y>
<MultiBinding Converter="{StaticResource AnimationFactorToValueConverter}" ConverterParameter="negative">
<Binding ElementName="ContentPresenterBorder" Path="ActualHeight" />
<Binding ElementName="ContentPresenterBorder" Path="Tag" />
<Binding ElementName="AnimationFactorBorder" Path="Width" />
</MultiBinding>
</TranslateTransform.Y>
</TranslateTransform>
<TranslateTransform x:Key="WidthAnimation">
<TranslateTransform x:Key="WidthAnimationNegative">
<TranslateTransform.X>
<MultiBinding Converter="{StaticResource AnimationFactorToValueConverter}" ConverterParameter="negative">
<Binding ElementName="ContentPresenterBorder" Path="ActualWidth" />
<Binding ElementName="ContentPresenterBorder" Path="Tag" />
<Binding ElementName="AnimationFactorBorder" Path="Width" />
</MultiBinding>
</TranslateTransform.X>
</TranslateTransform>
<TranslateTransform x:Key="HeightAnimationPositive">
<TranslateTransform.Y>
<MultiBinding Converter="{StaticResource AnimationFactorToValueConverter}" ConverterParameter="positive">
<Binding ElementName="ContentPresenterBorder" Path="ActualHeight" />
<Binding ElementName="AnimationFactorBorder" Path="Width" />
</MultiBinding>
</TranslateTransform.Y>
</TranslateTransform>
<TranslateTransform x:Key="WidthAnimationPositive">
<TranslateTransform.X>
<MultiBinding Converter="{StaticResource AnimationFactorToValueConverter}" ConverterParameter="positive">
<Binding ElementName="ContentPresenterBorder" Path="ActualWidth" />
<Binding ElementName="AnimationFactorBorder" Path="Width" />
</MultiBinding>
</TranslateTransform.X>
</TranslateTransform>
Expand All @@ -366,7 +381,7 @@
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="(Border.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="Tag">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimationFactorBorder" Storyboard.TargetProperty="Width">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1.0" />
<SplineDoubleKeyFrame
KeySpline="0.0, 0.0, 0.0, 1.0"
Expand All @@ -381,9 +396,9 @@
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="(Border.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
<DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="{x:Static Visibility.Collapsed}" />
<DiscreteObjectKeyFrame KeyTime="0:0:5" Value="{x:Static Visibility.Collapsed}" />
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="Tag">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimationFactorBorder" Storyboard.TargetProperty="Width">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0.0" />
<SplineDoubleKeyFrame
KeySpline="1.0, 1.0, 0.0, 1.0"
Expand Down Expand Up @@ -411,8 +426,8 @@
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="(Border.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="Tag">
<DiscreteDoubleKeyFrame KeyTime="0" Value="-1.0" />
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimationFactorBorder" Storyboard.TargetProperty="Width">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1.0" />
<SplineDoubleKeyFrame
KeySpline="0.0, 0.0, 0.0, 1.0"
KeyTime="0:0:0.333"
Expand All @@ -428,12 +443,12 @@
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
<DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="{x:Static Visibility.Collapsed}" />
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="Tag">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimationFactorBorder" Storyboard.TargetProperty="Width">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0.0" />
<SplineDoubleKeyFrame
KeySpline="1.0, 1.0, 0.0, 1.0"
KeyTime="0:0:0.333"
Value="-1.0" />
Value="1.0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
Expand All @@ -456,8 +471,8 @@
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="(Border.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="Tag">
<DiscreteDoubleKeyFrame KeyTime="0" Value="-1.0" />
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimationFactorBorder" Storyboard.TargetProperty="Width">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1.0" />
<SplineDoubleKeyFrame
KeySpline="0.0, 0.0, 0.0, 1.0"
KeyTime="0:0:0.333"
Expand All @@ -473,12 +488,12 @@
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
<DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="{x:Static Visibility.Collapsed}" />
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="Tag">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimationFactorBorder" Storyboard.TargetProperty="Width">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0.0" />
<SplineDoubleKeyFrame
KeySpline="1.0, 1.0, 0.0, 1.0"
KeyTime="0:0:0.333"
Value="-1.0" />
Value="1.0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
Expand All @@ -501,7 +516,7 @@
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="(Border.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="Tag">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimationFactorBorder" Storyboard.TargetProperty="Width">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1.0" />
<SplineDoubleKeyFrame
KeySpline="0.0, 0.0, 0.0, 1.0"
Expand All @@ -518,7 +533,7 @@
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
<DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="{x:Static Visibility.Collapsed}" />
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterBorder" Storyboard.TargetProperty="Tag">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimationFactorBorder" Storyboard.TargetProperty="Width">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0.0" />
<SplineDoubleKeyFrame
KeySpline="1.0, 1.0, 0.0, 1.0"
Expand All @@ -542,7 +557,7 @@
Value="{StaticResource DefaultExpanderToggleButtonRightStyle}"
TargetName="HeaderSite"/>
<Setter Property="RenderTransform"
Value="{DynamicResource WidthAnimation}"
Value="{DynamicResource WidthAnimationNegative}"
TargetName="ContentPresenterBorder" />
</Trigger>

Expand All @@ -558,14 +573,14 @@
Value="{StaticResource DefaultExpanderToggleButtonUpStyle}"
TargetName="HeaderSite"/>
<Setter Property="RenderTransform"
Value="{DynamicResource HeightAnimation}"
Value="{DynamicResource HeightAnimationPositive}"
TargetName="ContentPresenterBorder" />
</Trigger>

<Trigger Property="ExpandDirection"
Value="Down">
<Setter Property="RenderTransform"
Value="{DynamicResource HeightAnimation}"
Value="{DynamicResource HeightAnimationNegative}"
TargetName="ContentPresenterBorder" />
</Trigger>

Expand All @@ -581,7 +596,7 @@
Value="{StaticResource DefaultExpanderToggleButtonLeftStyle}"
TargetName="HeaderSite"/>
<Setter Property="RenderTransform"
Value="{DynamicResource WidthAnimation}"
Value="{DynamicResource WidthAnimationPositive}"
TargetName="ContentPresenterBorder" />
</Trigger>

Expand Down
Loading

0 comments on commit 53a7636

Please sign in to comment.