8

我目前有

<ContentControl
   Grid.Column="2" Grid.Row="3" 
   >
   <ContentControl.Triggers>
       <EventTrigger RoutedEvent="UIElement.MouseEnter">
           <BeginStoryboard 
               Storyboard="{StaticResource ShakeStatorMinorRadiusEdit}"/>
       </EventTrigger>
   </ContentControl.Triggers>

   ... <snip> ...

</ContentControl>

    <Grid.Resources>
         <Storyboard x:Key="ShakeStatorMinorRadiusEdit">
            <DoubleAnimationUsingKeyFrames
                        Storyboard.TargetName="StatorMinorRadiusEdit"
                        Storyboard.TargetProperty="RenderTransform.X" 
                        RepeatBehavior="5x"
                        >
                <EasingDoubleKeyFrame KeyTime="0:0:0.05" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="3"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.20" Value="-3"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
     </Grid.Resources>

在此处输入图像描述

这个想法是,当鼠标进入左侧黄色突出显示的控件时,右侧黄色突出显示的控件会抖动。右侧的控件具有 x:Name=StatorMinorRadiusEdit到目前为止,上述工作都很好。

现在我想添加另一个并发症。我只希望在我的视图模型RotorLobes == 1 上的值时触发动画。在想象的世界中我会这样做。

<ContentControl
   Grid.Column="2" Grid.Row="3" 
   >
   <ContentControl.Triggers>
       <EventTrigger RoutedEvent="UIElement.MouseEnter">

           <If Property="{Binding RotorLobes}" Value="1"/>

           <BeginStoryboard 
               Storyboard="{StaticResource ShakeStatorMinorRadiusEdit}"/>
       </EventTrigger>
   </ContentControl.Triggers>

   ... <snip> ...

</ContentControl>

在现实世界中,我不知道如何实现这一点。

4

1 回答 1

4

您可以尝试从 EventTrigger 更改为 MultiTrigger 并使用 IsMouseOver 属性而不是 MouseEnter 事件。但是,正如您所发现的,样式中的 Storyboard 不允许您指定 TargetName,因此将触发器移动到目标对象很重要。

下面的示例将 Rotor 的值硬编码为 1,您需要在 TargetProperty 中正确命名 RenderTransform,否则您将获得运行时异常。

<Grid>
        <Grid.Resources>
            <Storyboard x:Key="ShakeStatorMinorRadiusEdit">
                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)"
                                               RepeatBehavior="5x">
                    <EasingDoubleKeyFrame KeyTime="0:0:0.05"
                                          Value="0" />
                    <EasingDoubleKeyFrame KeyTime="0:0:0.1"
                                          Value="3" />
                    <EasingDoubleKeyFrame KeyTime="0:0:0.15"
                                          Value="0" />
                    <EasingDoubleKeyFrame KeyTime="0:0:0.20"
                                          Value="-3" />
                    <EasingDoubleKeyFrame KeyTime="0:0:0.25"
                                          Value="0" />
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
        </Grid.Resources>

        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Column="1">Rotor</TextBlock>
        <TextBlock Grid.Column="2">Stator</TextBlock>

        <TextBlock Grid.Column="0"
                   Grid.Row="1">Lobes</TextBlock>
        <TextBlock Grid.Column="0"
                   Grid.Row="2">Major Radius</TextBlock>
        <TextBlock Grid.Column="0"
                   Grid.Row="3">Minor Radius</TextBlock>

        <TextBox Name="RotorLobes"
                 Grid.Column="1"
                 Grid.Row="1"
                 Text="1" />
        <TextBox Grid.Column="1"
                 Grid.Row="2" />
        <TextBox Name="MinorRadiusRotor"
                 Background="Blue"
                 Grid.Column="1"
                 Grid.Row="3" />

        <TextBox Grid.Column="2"
                 Grid.Row="1" />
        <TextBox Grid.Column="2"
                 Grid.Row="2" />
        <TextBox Name="MinorRadiusStator"
                 Background="Green"
                 Grid.Column="2"
                 Grid.Row="3">
            <TextBox.Style>
                <Style>
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=MinorRadiusRotor, Path=IsMouseOver}"
                                           Value="True" />
                                <Condition Binding="{Binding ElementName=RotorLobes, Path=Text}}"
                                           Value="1" />
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource ShakeStatorMinorRadiusEdit}" />
                            </MultiDataTrigger.EnterActions>
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>

    </Grid>
于 2013-08-08T11:16:03.823 回答