ActualWidth as value of From WPF animation

倖福魔咒の 提交于 2019-12-04 04:27:37

问题


Why can't I refer to ActualWidth as a value from, which I was able to use in code.

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:


RelativeSource={RelativeSource Mode=Self} will probably be refering to the DoubleAnimation (which doesn't have an ActualWidth property). Try to find the ancestor Button instead

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>


来源:https://stackoverflow.com/questions/5154205/actualwidth-as-value-of-from-wpf-animation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!