如何检测文本框是否包含文本?

时间:2015-10-01 08:41:13

标签: c# wpf animation textbox storyboard

我有一个动画文本框,但如果文本框有文本,我希望它停止该动画,如果文本框没有文本,我希望它再次启动?但我不确定这是否可行,因为动画链接到另一个文本框?

TextBoxStyle1是动画。

TextBoxStyle2是文本输入的位置。

这是我的代码;

 <Window.Resources>

    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <local:TextInputToVisibilityConverter x:Key="TextInputToVisibilityConverter" />

    <Storyboard x:Key="StoryboardBorder">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border">
            <EasingColorKeyFrame KeyTime="0" Value="#FFABADB3"/>
            <EasingColorKeyFrame KeyTime="0:0:0.6" Value="#FF00BCD4"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>


    <FontFamily x:Key="MainFont">Arial</FontFamily>

    <SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/>
    <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FFC1C1C1"/>
    <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF00BCD4"/>


    <Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
        <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="FontFamily" Value="{StaticResource MainFont}"/>
        <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 TextBox}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="StoryboardTextAnimation">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="TextBox">
                                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="-23.333"/>
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="TextBox">
                                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0.76"/>
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="TextBox">
                                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0.76"/>
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="TextBox">
                                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="-25.597"/>
                            </DoubleAnimationUsingKeyFrames>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="TextBox">
                                <EasingColorKeyFrame KeyTime="0" Value="#FF8B8B8B"/>
                                <EasingColorKeyFrame KeyTime="0:0:0.2" Value="#FF00BCD4"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="StoryboardTextAnimation_Copy1">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="TextBox">
                                <SplineDoubleKeyFrame KeyTime="0" Value="-23.333"/>
                                <SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="TextBox">
                                <SplineDoubleKeyFrame KeyTime="0" Value="0.76"/>
                                <SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="1"/>
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="TextBox">
                                <SplineDoubleKeyFrame KeyTime="0" Value="0.76"/>
                                <SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="1"/>
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="TextBox">
                                <SplineDoubleKeyFrame KeyTime="0" Value="-25.597"/>
                                <SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
                            </DoubleAnimationUsingKeyFrames>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="TextBox">
                                <SplineColorKeyFrame KeyTime="0" Value="#FF00BCD4"/>
                                <SplineColorKeyFrame KeyTime="0:0:0.2" Value="#FFC1C1C1"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>

                    </ControlTemplate.Resources>

                    <Border x:Name="border" SnapsToDevicePixels="True" BorderThickness="0,0,0,2" BorderBrush="Transparent">
                        <ScrollViewer x:Name="TextBox" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" RenderTransformOrigin="0.5,0.5" Content="Floating Label Text" Background="White" Margin="0,3.75,0,3.25">
                            <ScrollViewer.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform/>
                                    <SkewTransform/>
                                    <RotateTransform/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </ScrollViewer.RenderTransform>
                        </ScrollViewer>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                            <Setter Property="Text" Value="{x:Null}" />

                        </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>

                        <Trigger Property="IsKeyboardFocused" Value="False">
                            <Setter Property="Text" Value="{x:Null}" />


                        </Trigger>


                        <DataTrigger Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}"
         Value="True">

                            <DataTrigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource StoryboardBorder}" />
                                <BeginStoryboard Storyboard="{StaticResource StoryboardTextAnimation}" />
                            </DataTrigger.EnterActions>

                            <DataTrigger.ExitActions>

                                <BeginStoryboard Storyboard="{StaticResource StoryboardTextAnimation_Copy1}" />
                            </DataTrigger.ExitActions>

                        </DataTrigger>


                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
                    <Condition Property="IsSelectionActive" Value="false"/>
                </MultiTrigger.Conditions>
                <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
            </MultiTrigger>
        </Style.Triggers>
    </Style>


    <SolidColorBrush x:Key="brushWatermarkForeground" Color="LightGray" />


    <SolidColorBrush x:Key="TextBox.MouseOver.Border2" Color="#FF7EB4EA"/>
    <Style x:Key="TextBoxStyleNew" TargetType="{x:Type TextBox}">
        <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 TextBox}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="StoryboardAnimateText"/>
                    </ControlTemplate.Resources>
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" BorderThickness="0,0,0,2" Margin="0,-5,0,0">
                        <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                    </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.Border2}"/>
                            <Setter Property="BorderThickness" TargetName="border" Value="0"/>
                        </Trigger>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
                            <Setter Property="BorderThickness" TargetName="border" Value="0"/>

                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
                    <Condition Property="IsSelectionActive" Value="false"/>
                </MultiTrigger.Conditions>
                <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
            </MultiTrigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="TextBlockStyle1" TargetType="{x:Type TextBlock}">
        <Setter Property="TextWrapping" Value="NoWrap"/>
        <Setter Property="TextTrimming" Value="None"/>
    </Style>
    <SolidColorBrush x:Key="TextBox.MouseOver.Border3" Color="#FF00BCD4"/>
    <SolidColorBrush x:Key="TextBox.Focus.Border2" Color="#FF00BCD4"/>


    <Style x:Key="TextBoxStyle2" TargetType="{x:Type TextBox}">
        <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 TextBox}">
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                    </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>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
                    <Condition Property="IsSelectionActive" Value="false"/>
                </MultiTrigger.Conditions>
                <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
            </MultiTrigger>
        </Style.Triggers>
    </Style>



</Window.Resources>



<Grid Name="grid1" Focusable="True">

    <Grid x:Name="TextBoxes" Margin="23.75,0,-23.75,0">
        <TextBlock Margin="250.449,182.112,374.044,0"   Text="Hint Text" Foreground="{StaticResource brushWatermarkForeground}"
               Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" Height="19.725" VerticalAlignment="Top" Style="{DynamicResource TextBlockStyle1}" />
        <TextBox Name="txtUserEntry"  Background="Transparent" BorderBrush="#FFC1C1C1" Margin="250.449,182.112,352.952,0" Height="25.689" VerticalAlignment="Top" BorderThickness="0,0,0,2" Style="{DynamicResource TextBoxStyle2}" />
        <TextBox x:Name="textBox1" Text="Floating Label Text"   Height="25.689" VerticalAlignment="Top" Margin="250.449,182.112,352.952,0" Style="{DynamicResource TextBoxStyle1}" BorderThickness="1" Foreground="#FFC1C1C1" Background="White" BorderBrush="#FFC1C1C1"/>
    </Grid>

</Grid>

1 个答案:

答案 0 :(得分:1)

不确定为什么选票下来,因为解决这个问题对于任何想要磨练他们的WPF风格/模板技能的人来说都是一个很好的练习。

创建样式/模板时要记住两件重要的事情,尤其是涉及触发器时。

  1. 触发器的顺序很重要。
  2. 如果你发现自己挠头,你需要从另一个角度看它并简化。
  3. 在你得到的那一刻,现在是开始制作custom control的好时机,事实上我在下面提供的例子就是这样。

    对于您尝试做的事情,您有3种可能的结果要进行测试。如果我们退后一步看看,我们可以确切地看到我们需要做什么,我会将其分解。

    1. IsFocused
      • 为边框着色并标记(无论如何都会发生这种情况)
    2. !IsFocused&amp; Text.Length = 0
      • ENTER:降低并调整标签大小。
      • 退出:提升并调整标签大小。
    3. IsFocused&amp; Text.Length = 0
      • ENTER:显示提示。
      • 退出:隐藏提示。
    4. 就是这样,那些是我们需要的唯一三个触发器,一个Trigger和两个MultiTriggers。你可以发挥创造力,只让它成为一个触发器,但可维护性和可读性将是可怕的。

      让我们从自定义控件的代码开始:

      public class AnimatedTextBox : TextBox
      {
          public static readonly DependencyProperty LabelProperty =
              DependencyProperty.Register("Label", typeof (string), typeof (AnimatedTextBox),
                  new FrameworkPropertyMetadata(null));
      
          public static readonly DependencyProperty HintProperty =
              DependencyProperty.Register("Hint", typeof (string), typeof (AnimatedTextBox),
                  new FrameworkPropertyMetadata(null));
      
          static AnimatedTextBox()
          {
              DefaultStyleKeyProperty.OverrideMetadata(typeof (AnimatedTextBox),
                  new FrameworkPropertyMetadata(typeof (AnimatedTextBox)));
          }
      
          public string Label
          {
              get { return (string) GetValue(LabelProperty); }
              set { SetValue(LabelProperty, value); }
          }
      
          public string Hint
          {
              get { return (string) GetValue(HintProperty); }
              set { SetValue(HintProperty, value); }
          }
      }
      

      我刚刚创建了Label和Hint字符串以保持简单,你可以将它们作为对象并扩展其中的可能性。

      现在风格:

      <Color x:Key="Color.Control.Border.Focus">#FF00BCD4</Color>
          <SolidColorBrush x:Key="SolidColorBrush.Control.Border" Color="#FFABADB3" />
          <SolidColorBrush x:Key="SolidColorBrush.Hint" Color="LightGray" />
      
          <Style TargetType="{x:Type local:AnimatedTextBox}">
              <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
              <Setter Property="BorderBrush" Value="{StaticResource SolidColorBrush.Control.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 local:AnimatedTextBox}">
                          <ControlTemplate.Resources />
                          <Border x:Name="Border"
                                  Background="{TemplateBinding Background}"
                                  BorderBrush="{TemplateBinding BorderBrush}"
                                  BorderThickness="0,0,0,2"
                                  SnapsToDevicePixels="True">
                              <StackPanel>
                                  <TextBlock x:Name="LabelTextBlock"
                                             Focusable="False"
                                             Foreground="{TemplateBinding BorderBrush}"
                                             RenderTransformOrigin="0.5,0.5"
                                             Text="{TemplateBinding Label}">
                                      <TextBlock.RenderTransform>
                                          <TransformGroup>
                                              <ScaleTransform ScaleX=".75" ScaleY=".75" />
                                              <TranslateTransform X="0" Y="0" />
                                          </TransformGroup>
                                      </TextBlock.RenderTransform>
                                  </TextBlock>
                                  <Grid>
                                      <ScrollViewer x:Name="PART_ContentHost"
                                                    Focusable="false"
                                                    HorizontalScrollBarVisibility="Hidden"
                                                    VerticalScrollBarVisibility="Hidden" />
                                      <TextBlock x:Name="HintTextBlock"
                                                 Margin="5 0 0 0"
                                                 Focusable="False"
                                                 Foreground="{StaticResource SolidColorBrush.Hint}"
                                                 IsHitTestVisible="False"
                                                 Opacity="0"
                                                 Text="{TemplateBinding Hint}" />
                                  </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}" />
                                          </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" />
                                          </Storyboard>
                                      </BeginStoryboard>
                                  </Trigger.ExitActions>
                              </Trigger>
                              <MultiDataTrigger>
                                  <MultiDataTrigger.Conditions>
                                      <Condition Binding="{Binding Path=Text.Length, RelativeSource={RelativeSource Self}}" Value="0" />
                                      <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"
                                                               Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
                                                               To="1" />
                                              <DoubleAnimation Duration="0:0:0.2"
                                                               Storyboard.TargetName="LabelTextBlock"
                                                               Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
                                                               To="1" />
                                              <DoubleAnimation Duration="0:0:0.2"
                                                               Storyboard.TargetName="LabelTextBlock"
                                                               Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.Y)"
                                                               To="15" />
                                          </Storyboard>
                                      </BeginStoryboard>
                                  </MultiDataTrigger.EnterActions>
                                  <MultiDataTrigger.ExitActions>
                                      <BeginStoryboard>
                                          <Storyboard>
                                              <DoubleAnimation Duration="0:0:0.2"
                                                               Storyboard.TargetName="LabelTextBlock"
                                                               Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" />
                                              <DoubleAnimation Duration="0:0:0.2"
                                                               Storyboard.TargetName="LabelTextBlock"
                                                               Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" />
                                              <DoubleAnimation Duration="0:0:0.2"
                                                               Storyboard.TargetName="LabelTextBlock"
                                                               Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.Y)" />
                                          </Storyboard>
                                      </BeginStoryboard>
                                  </MultiDataTrigger.ExitActions>
                              </MultiDataTrigger>
                              <MultiDataTrigger>
                                  <MultiDataTrigger.Conditions>
                                      <Condition Binding="{Binding Path=Text.Length, RelativeSource={RelativeSource Self}}" Value="0" />
                                      <Condition Binding="{Binding Path=IsFocused, RelativeSource={RelativeSource Self}}" Value="true" />
                                  </MultiDataTrigger.Conditions>
                                  <MultiDataTrigger.EnterActions>
                                      <BeginStoryboard>
                                          <Storyboard>
                                              <DoubleAnimation Duration="0:0:0.2"
                                                               Storyboard.TargetName="HintTextBlock"
                                                               Storyboard.TargetProperty="Opacity"
                                                               To="1" />
                                          </Storyboard>
                                      </BeginStoryboard>
                                  </MultiDataTrigger.EnterActions>
                                  <MultiDataTrigger.ExitActions>
                                      <BeginStoryboard>
                                          <Storyboard>
                                              <DoubleAnimation Duration="0:0:0.2"
                                                               Storyboard.TargetName="HintTextBlock"
                                                               Storyboard.TargetProperty="Opacity" />
                                          </Storyboard>
                                      </BeginStoryboard>
                                  </MultiDataTrigger.ExitActions>
                              </MultiDataTrigger>
                          </ControlTemplate.Triggers>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      

      风格是你拥有的95%,我只是将它组合成一个控件。我唯一真正改变的是,我将模板设置为在'IsFocused'和'HasText'状态下默认。我们的想法是我们的​​控制总是努力处于那种状态,因此更容易将其绊倒并将其设置为其他状态。还有一些ifs和buts反过来,所以编码最小公分母对我们有利。

      用法如下:

      <Grid>
          <StackPanel VerticalAlignment="Center">
              <local:AnimatedTextBox Width="300"
                                     Margin=" 0 0 0 15"
                                     HorizontalAlignment="Center"
                                     VerticalAlignment="Center"
                                     Hint="Hint 1"
                                     Label="Label 1" />
              <local:AnimatedTextBox Width="300"
                                     HorizontalAlignment="Center"
                                     VerticalAlignment="Center"
                                     Hint="Hint 2"
                                     Label="Label 2" />
          </StackPanel>
      </Grid>
      

      这可能并不完美,但希望能让你走上正轨。