4

I'm showing an expander in a modal window. If I don't toggle the expander the window will close fine. Once I've toggled it and close the window it throws an exception on the invocation of

System.Windows.Media.Animation.DoubleAnimationBase.GetCurrentValue(Object defaultOriginValue, Object defaultDestinationValue, AnimationClock animationClock)

I have the same control on another window which works fine.

I define the control as follows

 <Expander Template="{DynamicResource MoreInformationExpander}">
      <StackPanel>
          <TextBlock Text="{Binding MoreInformationText}  />
       </StackPanel>
 </Expander>

The template for the expander:

<ControlTemplate x:Key="MoreInformationExpander" TargetType="{x:Type Expander}">
    <DockPanel HorizontalAlignment="Stretch">
        <ToggleButton x:Name="ExpanderButton" 
                          DockPanel.Dock="Bottom"
                          Template="{StaticResource MoreInformationExpanderButton}"
                          IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
                          OverridesDefaultStyle="True"
                          HorizontalAlignment="Left">
        </ToggleButton>
        <ScrollViewer x:Name="ExpanderContentScrollView" DockPanel.Dock="Top"
                          HorizontalScrollBarVisibility="Hidden"
                          VerticalScrollBarVisibility="Hidden"
                          HorizontalAlignment="Stretch"
                          HorizontalContentAlignment="Stretch"
                          VerticalContentAlignment="Bottom"
                          Background="{DynamicResource WindowBorderGrayBrush}"
                          Padding="8,4,0,0">
            <ScrollViewer.Tag>
                <system:Double>0.0</system:Double>
            </ScrollViewer.Tag>
            <ScrollViewer.Height>
                <MultiBinding Converter="{StaticResource multiplyConverter}">
                    <Binding Path="ActualHeight" ElementName="ExpanderContent"/>
                    <Binding Path="Tag" RelativeSource="{RelativeSource Self}" />
                </MultiBinding>
            </ScrollViewer.Height>
            <ContentPresenter x:Name="ExpanderContent" ContentSource="Content"/>
        </ScrollViewer>
    </DockPanel>
    <ControlTemplate.Triggers>
        <Trigger Property="IsExpanded" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="ExpanderContentScrollView"
                                             Storyboard.TargetProperty="Tag"                                                 
                                             To="1"
                                             Duration="0:0:0.4"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="ExpanderContentScrollView"
                                             Storyboard.TargetProperty="Tag"                                                 
                                             To="0"
                                             Duration="0:0:0.4"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.ExitActions>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

The template for the Toggle Button:

    <ControlTemplate x:Key="MoreInformationExpanderButton" TargetType="{x:Type ToggleButton}">
    <Grid >
        <TextBlock Name="Tb" Foreground="{DynamicResource BlueSolidBrush}" FontSize="14" TextDecorations="Underline"/>
    </Grid>

    <ControlTemplate.Triggers>
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="Text" Value="Less Information"  TargetName="Tb" />
        </Trigger>
        <Trigger Property="IsChecked" Value="False">
            <Setter Property="Text" Value="More Information" TargetName="Tb" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Cursor" Value="Hand" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

The multiply converter:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        double result = 1.0;
        for (int i = 0; i < values.Length; i++)
        {
            if (values[i] is double)
            {
                result *= (double)values[i];
            }
        }

        return result;
    }

Exception and stack trace

An unhandled exception occurred, and the application is terminating. Error details: Cannot animate the 'Tag' property on a 'System.Windows.Controls.ScrollViewer' using a 'System.Windows.Media.Animation.DoubleAnimation'. For details see the inner exception.

=========================================================================

at System.Windows.Media.Animation.DoubleAnimationBase.GetCurrentValue(Object defaultOriginValue, Object defaultDestinationValue, AnimationClock animationClock) at System.Windows.Media.Animation.AnimationLayer.GetCurrentValue(Object defaultDestinationValue) at System.Windows.Media.Animation.AnimationStorage.GetCurrentPropertyValue(AnimationStorage storage, DependencyObject d, DependencyProperty dp, PropertyMetadata metadata, Object baseValue) at System.Windows.Media.Animation.AnimationStorage.OnCurrentTimeInvalidated(Object sender, EventArgs args)

user48408
  • 3,234
  • 11
  • 39
  • 59
  • 1
    Why are you doing animation on Tag property ? `` It also dont have `From` property assigned – Abin Feb 08 '16 at 15:38
  • I did try setting the From property since you mentioned it. This was a working expander elsewhere in our codebase which I've tried to re-use. Not the best reason to give. I'm certainly willing to try another approach and have been researching that – user48408 Feb 08 '16 at 15:59
  • Its the double animation which allows it to expand and collapse in a customised way – user48408 Feb 08 '16 at 16:14
  • 1
    @user48408 I tried your code and it works quite well here. I create lots of Windows containing your Expander, toggled them a bit, and close'em. No problem. Could you post your `multiplyConverter`? – Peter Feb 08 '16 at 18:27
  • Thanks Peter for taking a look. I've updated the question – user48408 Feb 09 '16 at 10:05

0 Answers0