RoutedEvent TextBox.LostFocus在我的WPF应用程序中不起作用

时间:2017-08-29 09:58:54

标签: wpf xaml storyboard eventtrigger

我正在使用Storyboard处理WPF动画。我想要的是在文本框获得焦点后立即淡出页面的所有内容,并在焦点从文本框中删除后立即淡出所有内容。为此,我有以下XAML,没有代码。

<Page.Triggers>
  <EventTrigger RoutedEvent="TextBox.GotFocus">
     <BeginStoryboard>
        <Storyboard>
           <DoubleAnimation
              Storyboard.TargetName="Grid1"
              Storyboard.TargetProperty="Opacity"
              From="1" To="0.1" Duration="0:0:0.2"
              AutoReverse="False" >
           </DoubleAnimation>
        </Storyboard>
     </BeginStoryboard>
  </EventTrigger>
  <EventTrigger RoutedEvent="TextBox.LostFocus">
     <BeginStoryboard>
        <Storyboard>
           <DoubleAnimation
              Storyboard.TargetName="Grid1"
              Storyboard.TargetProperty="Opacity"
              From="0.1" To="1" Duration="0:0:0.2"
              AutoReverse="False">
           </DoubleAnimation>
        </Storyboard>
     </BeginStoryboard>
  </EventTrigger>

要将焦点移出文本框,我有以下按钮:

<Button
        Name="SearchButton" 
        Height="30" Width="30"
        Grid.Column="1"
        Focusable="True"
        IsHitTestVisible="True"
        Style="{StaticResource SearchButton}"
        Padding="3,3,3,3"
        Margin="3,3,3,3" Click="Button_Click"/>

当我运行应用程序时,单击文本框可使淡出效果正常。但是当我点击按钮时,淡入还没有开始。

任何人都可以给我一些见解吗?

1 个答案:

答案 0 :(得分:0)

您应该将触发器直接放在TextBox,而不是Page级别:

  <TextBox>
    <TextBox.Triggers>
      <EventTrigger RoutedEvent="TextBox.GotFocus">
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation
               Storyboard.TargetName="Grid1"
               Storyboard.TargetProperty="Opacity"
               From="1" To="0.1" Duration="0:0:0.2"
               FillBehavior="HoldEnd">
            </DoubleAnimation>
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
      <EventTrigger RoutedEvent="TextBox.LostFocus">
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation
               Storyboard.TargetName="Grid1"
               Storyboard.TargetProperty="Opacity"
               From="0.1" To="1" Duration="0:0:0.2"
               FillBehavior="Stop">
            </DoubleAnimation>
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
    </TextBox.Triggers>
  </TextBox>

否则,由于这些事件的冒泡策略,将为GotFocus上的每个LostFocus的每个UIElementPage路由事件触发故事板。< / p>