0

我有一个具有以下 ListBoxItem 样式的自定义 ListBox。它包含一些动画以在鼠标悬停在 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 不被调用的原因吗?如何在控件本身上应用相同的动画?

4

1 回答 1

3

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

相反,使用LayoutTransform。它确实影响测量...

希望这可以帮助,

干杯,安瓦卡

于 2010-02-24T14:30:31.570 回答