Xaml 通过绑定更改文本框文本时启动情节提要

Xaml 通过绑定更改文本框文本时启动情节提要,xaml,triggers,textbox,storyboard,uwp,Xaml,Triggers,Textbox,Storyboard,Uwp,我正在为Windows(W10+WP10)编写一个通用应用程序,希望启动一个故事板,然后显示的文本(绑定文本)发生了更改 第一次尝试: <TextBlock Text="{Binding ASH}"> <TextBox.Triggers> <EventTrigger RoutedEvent="Binding.TargetUpdated" > <BeginStoryboard > <Storyboard> &

我正在为Windows(W10+WP10)编写一个通用应用程序,希望启动一个故事板,然后显示的文本(绑定文本)发生了更改

第一次尝试:

<TextBlock Text="{Binding ASH}">
 <TextBox.Triggers>
  <EventTrigger RoutedEvent="Binding.TargetUpdated" >
   <BeginStoryboard >
    <Storyboard>
     <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" From="Red" To="Transparent" Duration="0:1:0" />
    </Storyboard>
   </BeginStoryboard>
  </EventTrigger>
 </TextBox.Triggers>
</TextBlock>

第二次尝试:

<TextBlock Text="{Binding ASH}">
 <TextBox.Triggers>
  <EventTrigger RoutedEvent="TextBlock.DataContextChanged" >
   <BeginStoryboard >
    <Storyboard>
     <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" From="Red" To="Transparent" Duration="0:1:0" />
    </Storyboard>
   </BeginStoryboard>
  </EventTrigger>
 </TextBox.Triggers>
</TextBlock>

谁有主意,如何开始故事板


谢谢。

EventTrigger在UWP应用程序中不受支持,正如@Chris W所说,如果您只想使用XAML来实现这一点,您可以使用ControlStoryboardAction行为

但在使用应用程序之前,必须将
Microsoft.Xaml.Behaviors.Uwp.Managed
包添加到应用程序的引用中。要添加此引用,可以从中获取

您还可以使用
x:Bind
和代码隐藏来执行此操作。我在下面编写了一个示例,第一个名为“txt”的
文本框使用
x:Bind
,第二个名为“DataTriggerTB”的
文本框使用ControlStoryboard动作行为

XAML:


但是如果您使用的是MVVM模式,
this.std.Begin()x:Bind
方法中的code>将打破MVVM模式。

是否只需要XAML并安装行为包?您可以使用ControlStoryboard动作行为。
<Page
    x:Class="StoryBoardApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:StoryBoardApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" 
    xmlns:Interactions="using:Microsoft.Xaml.Interactions.Core"
    xmlns:Media="using:Microsoft.Xaml.Interactions.Media"
    mc:Ignorable="d">

    <Page.Resources>
        <Storyboard x:Name="StoryboardSample">
            <ColorAnimation Duration="0:0:3" To="Transparent" From="Red"
                Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                Storyboard.TargetName="DataTriggerTB" />
        </Storyboard>
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.Resources>
            <Storyboard x:Key="std" x:Name="std" >
                <ColorAnimation Storyboard.TargetName="brush" Storyboard.TargetProperty="Color" From="Red" To="Transparent" Duration="0:0:3" />
            </Storyboard>
        </Grid.Resources>
        <TextBox x:Name="txt"  Text="{x:Bind text, Mode=TwoWay}" Width="300" Height="200" >
            <TextBox.Background>
                <SolidColorBrush x:Name="brush"/>
            </TextBox.Background>
        </TextBox>


        <TextBox VerticalAlignment="Bottom" Width="300" Height="200" x:Name="DataTriggerTB">
            <Interactivity:Interaction.Behaviors>
                <Interactions:DataTriggerBehavior Binding="{Binding Text, ElementName=DataTriggerTB}" ComparisonCondition="NotEqual" Value="{Binding Text, ElementName=DataTriggerTB}">
                    <Media:ControlStoryboardAction Storyboard="{StaticResource StoryboardSample}" />
                </Interactions:DataTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
        </TextBox>
    </Grid>
</Page>