在密码值WPF上停止动画

时间:2015-11-20 14:25:31

标签: c# wpf xaml animation passwordbox

我在文本块上有一个动画 - “labelTextBlock”,它是文本块聚焦时的密码框的一部分..但是当密码框有一个值时,我希望停止这个动画。

我用文本框管理了这个但我明白这与密码框完全不同。我尝试使用下面的代码,并将动画放入资源但这导致了这个错误.. 附加信息:在......的名称范围内找不到'Border'名称。

以下代码..

private void pwdBox_PasswordChanged(object sender, RoutedEventArgs e)
{

    TextBlock LabelTextBlock = pwdBox.Template.FindName("LabelTextBlock", pwdBox) as TextBlock;
    if (pwdBox.SecurePassword.Length == 1)
    {
       Storyboard LabelStoryboard2 = this.Resources["LabelStoryboard2"] as Storyboard;
       LabelStoryboard2.Stop();
    }
    else
    {
       Storyboard LabelStoryboard2 = this.Resources["LabelStoryboard2"] as Storyboard;
       LabelStoryboard2.Begin();
    }
}

这是主要代码。

 <Style x:Key="PasswordBoxStyle1" TargetType="{x:Type PasswordBox}">
        <Setter Property="PasswordChar" Value="●"/>
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type PasswordBox}">
                    <ControlTemplate.Resources>

                    </ControlTemplate.Resources>
                    <Border x:Name="Border"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="0,0,0,2"
                        SnapsToDevicePixels="True" Margin="0,0,0,-8.5">
                        <StackPanel Margin="0,3.375,0,0">
                            <TextBlock x:Name="LabelTextBlock"
                                Focusable="False"
                                FontSize="16"
                                Foreground="{TemplateBinding BorderBrush}"
                                RenderTransformOrigin="0.5,0.5"
                                Text="Password Label" HorizontalAlignment="Stretch" Height="21.125" Panel.ZIndex="9">
                                <TextBlock.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </TextBlock.RenderTransform>
                            </TextBlock>
                            <Grid>
                                <ScrollViewer x:Name="PART_ContentHost"
                                    Focusable="false"
                                    HorizontalScrollBarVisibility="Hidden"
                                    VerticalScrollBarVisibility="Hidden" Margin="0,-16.75,-1,0" Height="15.96" VerticalAlignment="Top" d:LayoutOverrides="VerticalAlignment" />
                                <TextBlock x:Name="HintTextBlock"
                                    Margin="0.75,-18.125,4.25,-1.375"
                                    Focusable="False"
                                    FontSize="14"     
                                    Foreground="{StaticResource SolidColorBrush.Hint}"
                                    IsHitTestVisible="False"
                                    Opacity="0"
                                    Text="Enter Your Password" Height="20.96" VerticalAlignment="Top" >



                                </TextBlock>
                            </Grid>
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>

                        <Trigger Property="IsFocused" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation Duration="0:0:0.6"
                                                    Storyboard.TargetProperty="BorderBrush.Color"
                                                    To="{StaticResource Color.Control.Border.Focus}" />
                                        <ColorAnimation Duration="0:0:0.6"
                                                    Storyboard.TargetName="LabelTextBlock"
                                                    Storyboard.TargetProperty="Foreground.Color"
                                                    To="{StaticResource Color.Control.Border.Focus}" />

                                        <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="LabelTextBlock" From="16" To="11" Storyboard.TargetProperty="FontSize"/>
                                        <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="HintTextBlock" From="0" To="1" Storyboard.TargetProperty="Opacity"/>

                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="LabelTextBlock">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="-0.083"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="LabelTextBlock">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="-9.875"/>
                                        </DoubleAnimationUsingKeyFrames>

                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation Duration="0:0:0.6" Storyboard.TargetProperty="BorderBrush.Color" />
                                        <ColorAnimation Duration="0:0:0.6"
                                                    Storyboard.TargetName="LabelTextBlock"
                                                    Storyboard.TargetProperty="Foreground.Color" />
                                        <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="LabelTextBlock" From="11" To="16" Storyboard.TargetProperty="FontSize"/>
                                        <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="HintTextBlock" From="1" To="0" Storyboard.TargetProperty="Opacity"/>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="LabelTextBlock">
                                            <SplineDoubleKeyFrame KeyTime="0" Value="-0.083"/>
                                            <SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="LabelTextBlock">
                                            <SplineDoubleKeyFrame KeyTime="0" Value="-9.875"/>
                                            <SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
                                        </DoubleAnimationUsingKeyFrames>


                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>


                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>


<Grid Name="grid1" Focusable="True" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="94">
    <StackPanel Margin="0,0,-394.167,-152" Height="64" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="458.167" d:LayoutOverrides="VerticalAlignment">
        <PasswordBox Height="20" Name="pwdBox" PasswordChanged="pwdBox_PasswordChanged"  Margin="151.583,0,151.583,20" Width="155" Style="{DynamicResource PasswordBoxStyle1}" d:LayoutOverrides="LeftMargin, RightMargin"/>
    </StackPanel>

</Grid>

我不确定是否需要删除Isfocused触发器并将其添加到C#中?抱歉,如果我错过了什么。

编辑,我也尝试过这样做?但什么都不做?

   <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition  Binding="{Binding Path=Password}" Value="{x:Null}" />
                                <Condition Binding="{Binding Path=IsFocused, RelativeSource={RelativeSource Self}}" Value="false" />
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="LabelTextBlock" From="9" To="14" Storyboard.TargetProperty="FontSize"/>

                                    </Storyboard>
                                </BeginStoryboard>
                            </MultiDataTrigger.EnterActions>
                            <MultiDataTrigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="LabelTextBlock" From="14" To="9" Storyboard.TargetProperty="FontSize"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </MultiDataTrigger.ExitActions>
                        </MultiDataTrigger>

0 个答案:

没有答案