在XAML中使用相同属性的MultiTriggers

时间:2018-11-29 13:14:38

标签: c# wpf xaml storyboard multitrigger

我实际上是覆盖ControlTemplate的默认TextBox,并且要执行我想要的操作,当TextBox失去焦点并且是文本时,我需要触发两个动画是空的。为此,我使用具有两个条件的MultiTrigger

(我将在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)时,在{{1} }动画不再触发。

,如果我将整个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事件。 我在msdn上看到了EnterActions does not apply to the EventTrigger,所以我试图摆脱我的两个LostFocus,现在它可以工作了。

问题是我使用了EventTriggers,因为我需要检查MultiTrigger上的Text是否为空,但是也许有一种方法可以将该条件放入{ {1}}?

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

EventTriggers

问题在于,两个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触发器,则可以使用,但是我确实需要区分使用空白文本和不使用空白文本来松开焦点的情况。 关于如何实现这一目标的任何建议?

0 个答案:

没有答案