问题
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