窗口关闭时WPF Expander ScrollViewer抛出异常

时间:2016-02-08 14:59:56

标签: c# .net wpf unhandled-exception expander

我在模态窗口中显示扩展器。如果我不切换扩展器,窗口将关闭。一旦我切换它并关闭窗口,就会在调用

时抛出异常
System.Windows.Media.Animation.DoubleAnimationBase.GetCurrentValue(Object defaultOriginValue, Object defaultDestinationValue, AnimationClock animationClock)

我在另一个窗口上具有相同的控制功能。

我将控件定义如下

 <Expander Template="{DynamicResource MoreInformationExpander}">
      <StackPanel>
          <TextBlock Text="{Binding MoreInformationText}  />
       </StackPanel>
 </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>

切换按钮的模板:

    <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>

乘法转换器:

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;
    }

异常和堆栈跟踪

发生未处理的异常,应用程序正在终止。 错误详情: 无法使用'System.Windows.Media.Animation.DoubleAnimation'为'System.Windows.Controls.ScrollViewer'上的'Tag'属性设置动画。有关详细信息,请参阅内部异常。

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

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

0 个答案:

没有答案