1

我实际上覆盖了默认值ControlTemplateTextBox并且为了做我想做的事,我需要在TextBox失去焦点并且它的文本为空时触发两个动画。为了做到这一点,我正在使用MultiTrigger具有这样两个条件的 a :

FontSize(我将在示例中使用动画)

<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="IsFocused" Value="False"/>
    <Condition Property="Text" Value="{x:Static sys:String.Empty}"/>
  </MultiTrigger.Conditions>
  <MultiTrigger.EnterActions>
    <BeginStoryboard>
      <Storyboard>
        <DoubleAnimation Storyboard.TargetName="Title" Storyboard.TargetProperty="FontSize" From="9" To="120" Duration="0:0:2">
          <DoubleAnimation.EasingFunction>
            <QuinticEase EasingMode="EaseInOut"/>
          </DoubleAnimation.EasingFunction>
        </DoubleAnimation>
      </Storyboard>
    </BeginStoryboard>
  </MultiTrigger.EnterActions>
</MultiTrigger>

问题是,动画是在TextBox实例化时触发的,因为TextBox没有聚焦并且Text是空的。所以我们可以说动画和动画MultiTrigger都在工作,但是当我关注TextBox一次(GotFocus重置FontSize)时,LostFocus动画不再被触发。

但是如果我用这个替换整个MultiTrigger.EnterActions

<MultiTrigger.Setters>
  <Setter Property="Background" Value="Red"/>
</MultiTrigger.Setters>

一切都像魅力一样运作。我的意思是背景在开始时是红色的,然后是白色的,GotFocus然后是红色的LostFocus

我可能会误解我们必须使用的方式,EnterActions但那里没有那么多文档MultiTriggers。你知道为什么第一次之后动画不再触发吗?

顺便说一句,有没有比Value="{x:Static sys:String.Empty}"检查Text属性是否为空更好的方法?

编辑:ControlTemplate.Triggers已经在使用两个EventTriggers,一个路由到GotFocus事件,另一个路由到LostFocus事件。我在 msdn 上看到EnterActions不适用于 EventTrigger,所以我试图摆脱我的两者,EventTriggers现在它可以工作了。

问题是我使用了 a MultiTrigger,因为我需要检查 the 上是否Text为空,LostFocus但可能有办法将该条件放入EventTrigger?

编辑#2: 正如@Satish Pai 所问,这是ControlTemplate我用EventTriggersa替换的完整内容,MutliTrigger因为正如我所说,它们似乎不兼容。

<ControlTemplate TargetType="{x:Type TextBox}">
    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="14"/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock x:Name="Title" Text="{TemplateBinding ToolTip}" Margin="8,14,0,0" Grid.Row="0" Grid.RowSpan="2" Foreground="{StaticResource TextBox.Static.Border}"></TextBlock>
            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Grid.Row="1"/>
            <Rectangle x:Name="UnfocusedUnderLine" Fill="{StaticResource TextBox.Static.Border}" Height="6" Margin="0,1,0,0" Grid.Row="2"/>
            <Rectangle x:Name="UnderLine" Fill="{StaticResource TextBox.Focus.Border}" Height="6" Margin="0,1,0,0" Grid.Row="2" Width="0"/>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Opacity" TargetName="border" Value="0.56"/>
        </Trigger>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/>
        </Trigger>
        <Trigger Property="IsKeyboardFocused" Value="true">
            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
        </Trigger>
        <MultiTrigger x:Name="FocusChanged">
            <MultiTrigger.Conditions>
                <Condition Property="IsFocused" Value="True"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.EnterActions> <!--GotFocus-->
                <BeginStoryboard>
                    <Storyboard>
                        <!--Change UnderLine Color-->
                        <DoubleAnimation Storyboard.TargetName="UnderLine" Storyboard.TargetProperty="Width" From="0" To="500" Duration="0:0:0.25">
                            <DoubleAnimation.EasingFunction>
                                <QuinticEase EasingMode="EaseInOut"/>
                            </DoubleAnimation.EasingFunction>
                        </DoubleAnimation>


                    <!--Move Title Up-->
                    <ThicknessAnimation From="8,14,0,0" To="0,0,0,0" Duration="0:0:0.25" Storyboard.TargetName="Title" Storyboard.TargetProperty="Margin" AutoReverse="False" >
                        <ThicknessAnimation.EasingFunction>
                            <QuinticEase EasingMode="EaseInOut"/>
                        </ThicknessAnimation.EasingFunction>
                    </ThicknessAnimation>

                    <!--Decrease Title Size-->
                    <DoubleAnimation Storyboard.TargetName="Title"
                                     Storyboard.TargetProperty="FontSize"
                                     From="12"
                                     To="9"
                                     Duration="0:0:0.25">
                        <DoubleAnimation.EasingFunction>
                            <QuinticEase EasingMode="EaseInOut"/>
                        </DoubleAnimation.EasingFunction>
                    </DoubleAnimation>
                </Storyboard>
            </BeginStoryboard>
        </MultiTrigger.EnterActions>
        <MultiTrigger.ExitActions>  <!--LostFocus-->
            <BeginStoryboard>
                <Storyboard>

                    <!--Change UnderLine Color-->
                    <DoubleAnimation Storyboard.TargetName="UnderLine"
                                     Storyboard.TargetProperty="Width"
                                     From="500"
                                     To="0"
                                     Duration="0:0:0.2">
                        <DoubleAnimation.EasingFunction>
                            <QuinticEase EasingMode="EaseInOut"/>
                        </DoubleAnimation.EasingFunction>
                    </DoubleAnimation>
                </Storyboard>
            </BeginStoryboard>
        </MultiTrigger.ExitActions>
    </MultiTrigger>
    <MultiTrigger x:Name="LostFocusAndEmptyText">
        <MultiTrigger.Conditions>
            <Condition Property="Text" Value="{x:Static sys:String.Empty}"/>
        </MultiTrigger.Conditions>
        <MultiTrigger.EnterActions>
            <BeginStoryboard>
                <Storyboard>
                    <!--Move Title Down-->
                    <ThicknessAnimation From="0,0,0,0" To="8,14,0,0" Duration="0:0:0.2" Storyboard.TargetName="Title" Storyboard.TargetProperty="Margin" AutoReverse="False" >
                        <ThicknessAnimation.EasingFunction>
                            <QuinticEase EasingMode="EaseInOut"/>
                        </ThicknessAnimation.EasingFunction>
                    </ThicknessAnimation>

                    <!--Increase Title Size-->
                    <DoubleAnimation Storyboard.TargetName="Title"
                                     Storyboard.TargetProperty="FontSize"
                                     From="9"
                                     To="12"
                                     Duration="0:0:0.2">
                        <DoubleAnimation.EasingFunction>
                            <QuinticEase EasingMode="EaseInOut"/>
                        </DoubleAnimation.EasingFunction>
                    </DoubleAnimation>
                </Storyboard>
            </BeginStoryboard>
        </MultiTrigger.EnterActions>
    </MultiTrigger>
    <!--LostFocusWithEmptyText-->
</ControlTemplate.Triggers>
</ControlTemplate>

问题是他们都指的MultiTiggers是同一个IsFocused财产,他们不想一起工作。如果我删除LostFocusAndEmptyText触发器它可以工作,但我真的需要区分我用空文本和没有焦点的情况。关于如何实现这一目标的任何建议?

4

0 回答 0