情节提要仅触发一次第二条件

时间:2018-11-22 15:54:30

标签: c# wpf xaml storyboard

我有一个可以单击的按钮,TextBlock MultiDataTrigger运行,但是在每个按钮上单击它只运行两次。

说明::首先断开默认的TextBlock文本,然后有两个MultiDataTriggers满足条件运行,然后再次单击另一个MultiDataTrigger,但此后没有Storyboard动画起作用。

文本块

<TextBlock x:Name="abctxt" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Text="Disconnected">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=sbbtn, Path=IsPressed}" Value="True" />
                        <Condition Binding="{Binding ElementName=abctxt, Path=Text}" Value="Connected" />
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource DisconnectedTextChange}"/>
                    </MultiDataTrigger.EnterActions>
                </MultiDataTrigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=sbbtn, Path=IsPressed}" Value="True" />
                        <Condition Binding="{Binding ElementName=abctxt, Path=Text}" Value="Disconnected" />
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource ConnectedTextChange}"/>
                    </MultiDataTrigger.EnterActions>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

按钮

<Button x:Name="sbbtn" />

故事板

<Storyboard x:Key="ConnectedTextChange" x:Name="ConnectedTextChange">
    <StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" AccelerationRatio="1">
        <DiscreteStringKeyFrame KeyTime="0:0:0.8" Value="Connected"/>
    </StringAnimationUsingKeyFrames>
</Storyboard>

<Storyboard x:Key="DisconnectedTextChange" x:Name="DisconnectedTextChange">
    <StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" AccelerationRatio="1">
        <DiscreteStringKeyFrame KeyTime="0:0:0.8" Value="Disconnected"/>
    </StringAnimationUsingKeyFrames>
</Storyboard>

1 个答案:

答案 0 :(得分:1)

单个条件为IsChecked,而不是检查文本框的文本或按钮, 不用简单的Button,而要使用ToggleButton并在其上应用您的Button样式。

您可以这样使用它:

您的TextBlock

<TextBlock x:Name="abctxt" Text="Disconnected" />

ToggleButton而不是Button

<ToggleButton x:Name="sbbtn"> <!-- Also Apply Your Style For making your ToggleButton looks like your Button -->
<ToggleButton.Triggers>
    <EventTrigger RoutedEvent="ToggleButton.Checked">
        <BeginStoryboard Storyboard="{StaticResource ConnectedTextChange}" />
    </EventTrigger>
    <EventTrigger RoutedEvent="ToggleButton.Unchecked">
        <BeginStoryboard Storyboard="{StaticResource DisconnectedTextChange}" />
    </EventTrigger>
    </ToggleButton.Triggers>

您的Storyboard

<!-- Here i set TargetName also -->
<Storyboard x:Key="ConnectedTextChange" x:Name="ConnectedTextChange" Storyboard.TargetName="abctxt">
<StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" AccelerationRatio="1">
    <DiscreteStringKeyFrame KeyTime="0:0:0.8" Value="Connected"/>
</StringAnimationUsingKeyFrames>

<Storyboard x:Key="DisconnectedTextChange" x:Name="DisconnectedTextChange" Storyboard.TargetName="abctxt">
    <StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" AccelerationRatio="1">
        <DiscreteStringKeyFrame KeyTime="0:0:0.8" Value="Disconnected"/>
    </StringAnimationUsingKeyFrames>
</Storyboard>