在动画期间或之后不会调用WPF InvalidateMeasure

时间:2010-02-24 14:21:04

标签: wpf animation storyboard measureoverride

我有一个自定义ListBox,带有以下ListBoxItem样式。它包含一些动画,用于在鼠标悬停在ListBoxItem上时缩放ListBoxItem。

<Style x:Key="notesListBoxStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Grid x:Name="gridItem" 
                          Background="LemonChiffon" 
                          Height="100"
                          Width="100" 
                          RenderTransformOrigin="{TemplateBinding RenderTransformOrigin}">
                    <Grid.RenderTransform>
                        <ScaleTransform ScaleX="1.0"
                                        ScaleY="1.0"/>
                    </Grid.RenderTransform>
                    <Grid.LayoutTransform>
                        <RotateTransform Angle="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Converter={StaticResource listIndexConverter}, ConverterParameter='Rotate'}"/>
                    </Grid.LayoutTransform>
                    <Border BorderBrush="DarkGoldenrod" BorderThickness="2" Margin="2">
                        <ContentPresenter HorizontalAlignment="Stretch" 
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                          VerticalAlignment="Stretch"/>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="ListBoxItem.MouseEnter">
                        <BeginStoryboard Name="showItemStoryboard">
                            <Storyboard >
                                <DoubleAnimation Storyboard.TargetName="gridItem" 
                                                 Storyboard.TargetProperty="RenderTransform.ScaleX"
                                                 From="1.0"
                                                 To="1.5" 
                                                 Duration="0:0:0.3"/>
                                <DoubleAnimation Storyboard.TargetName="gridItem" 
                                                 Storyboard.TargetProperty="RenderTransform.ScaleY"
                                                 From="1.0"
                                                 To="1.5" 
                                                 Duration="0:0:0.3"/>
                                <DoubleAnimation Storyboard.TargetName="gridItem" 
                                                 Storyboard.TargetProperty="Width"
                                                 From="100"
                                                 To="400" 
                                                 Duration="0:0:0.3"/>
                                <DoubleAnimation Storyboard.TargetName="gridItem" 
                                                 Storyboard.TargetProperty="Height"
                                                 From="100"
                                                 To="200" 
                                                 Duration="0:0:0.3"/>
                                <DoubleAnimation Storyboard.TargetName="gridItem" 
                                                 Storyboard.TargetProperty="LayoutTransform.Angle"
                                                 To="0" 
                                                 Duration="0:0:0.3"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="ListBoxItem.MouseLeave">
                        <StopStoryboard BeginStoryboardName="showItemStoryboard"/>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

对于我的ListBox,我有自己的MeasureOverride实现,它被调用并且一切都很好,除了在ListBoxItem缩放之前没有调用MeasureOverride,所以它请求的尺寸小于所需的尺寸。

我在动画完成后尝试在ListBox上调用InvalidateMeasure但是没有触发MeasureOverride方法。目前我正在我的ControlTemplate中应用Grid上的动画,这是什么导致MeasureOverride不被调用?如何在控件本身上应用相同的动画?

1 个答案:

答案 0 :(得分:3)

RenderTransform修改元素的外观,但在布局传递完成后应用。这意味着使用RenderTransform不会产生布局传递。

相反,请使用LayoutTransform。它确实会影响衡量标准......

希望这有帮助,

干杯,安瓦卡