ActualWidth 作为 From WPF 动画的值

发布于 2024-10-20 03:43:45 字数 878 浏览 1 评论 0原文

为什么我不能将 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惟欲睡 2024-10-27 03:43:45

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文