开发者

WPF Focus Animation not working properly

开发者 https://www.devze.com 2023-01-05 10:54 出处:网络
I have a WPF usercontrol set up in XAML with the following Triggers: <UserControl.Triggers> <EventTrigger RoutedEvent=\"GotFocus\">

I have a WPF usercontrol set up in XAML with the following Triggers:

   <UserControl.Triggers>
        <EventTrigger RoutedEvent="GotFocus">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation To="1.5" Storyboard.TargetName="ImageContent" Stor开发者_运维知识库yboard.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>

The Animation is working fine but unfortunately the Triggers arent working as expected. The GotFocus trigger only fires if i Right-Click my Control or if i Click a Button that is contained within the UserControl.

I have created an EventHandler for the GotFocus event in code and that seems to fire in the right places.

Edit: If I change it to Fire on MouseEnter / Leave it also works as expected

Edit2: I foudn out that this behaviour is surfacing because the Control gets focus, but loses the focus again right away. I fixed it by adding some code, that checks if the source of the Event is the UserControl or something else.

Any hints on why this is happening would be appreciated.


Try settings Focusable="True" on the UserControl.

0

精彩评论

暂无评论...
验证码 换一张
取 消