我在XAML中设置了一个WPF用户控件,其中包含以下触发器:
<UserControl.Triggers>
<EventTrigger RoutedEvent="GotFocus">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="1.5" Storyboard.TargetName="ImageContent" Storyboard.TargetProperty="RenderTransform.Children[1].ScaleX" Duration="0:0:0.8" />
<DoubleAnimation To="1.5" Storyboard.TargetName="ImageContent" Storyboard.TargetProperty="RenderTransform.Children[1].ScaleY" Duration="0:0:0.8" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="LostFocus">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="1" Storyboard.TargetName="ImageContent" Storyboard.TargetProperty="RenderTransform.Children[1].ScaleX" Duration="0:0:0.8" />
<DoubleAnimation To="1" Storyboard.TargetName="ImageContent" Storyboard.TargetProperty="RenderTransform.Children[1].ScaleY" Duration="0:0:0.8" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</UserControl.Triggers>
动画工作正常但不幸的是触发器没有按预期工作。如果我右键单击我的控件或者如果我单击UserControl中包含的按钮,则只会触发GotFocus触发器。
我已经在代码中为GotFocus事件创建了一个EventHandler,它似乎在正确的位置触发。
编辑:如果我在MouseEnter / Leave上将其更改为Fire,它也可以按预期工作
Edit2:我发现这种行为正在浮出水面,因为Control会获得焦点,但会立即再次失去焦点。我通过添加一些代码来修复它,它检查Event的源是UserControl还是其他东西。
任何关于为什么会发生这种情况的提示都将受到赞赏。
答案 0 :(得分:0)
在UserControl上尝试设置Focusable =“True”。