WPF动画中ActualWidth的值是什么?

4

为什么我无法使用代码中可以使用的值,将ActualWidth作为一个参考呢?

XAML:

    <StackPanel>
        <Button x:Name="cmdVibrate" HorizontalAlignment="Center">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.MouseEnter">
                    <BeginStoryboard>
                        <Storyboard >
                            <DoubleAnimation From="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualWidth}" By="200" AutoReverse="True" Duration="0:0:1" Storyboard.TargetProperty="Width"></DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
            Mouse over me to shake
        </Button>
    </StackPanel>
</Window>
1个回答

9

RelativeSource={RelativeSource Mode=Self} 可能会引用 DoubleAnimation(它没有一个 ActualWidth 属性)。尝试找到祖先元素 Button

From="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}},
               Path=ActualWidth}"

Xaml

<StackPanel>
    <Button x:Name="cmdVibrate" HorizontalAlignment="Center">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.MouseEnter">
                <BeginStoryboard>
                    <Storyboard >
                        <DoubleAnimation From="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=ActualWidth}"
                                         By="200"
                                         AutoReverse="True"
                                         Duration="0:0:1"
                                         Storyboard.TargetProperty="Width"></DoubleAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
        Mouse over me to shake
    </Button>
</StackPanel>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接