wpf 将文本框内容从“Hello”变为无

2fjabf4q  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(108)

所以我玩了一点C# WPF动画。我需要帮助我知道如何动画的价值观,如颜色,宽度,位置等。
但是我不明白如何改变动画中的内容。我想在聚焦文本框时淡出文本框内容。我找不到故事板。setTargetProperty(changeContent?)的文本内容。
这里我的代码thsi动画宽度:

private void onGotFocusLoginID(Object sender, EventArgs e) {

            if(txtLoginID.Text.Equals("deineMail@mail.de")) {
                DoubleAnimation doubleAnimation = new DoubleAnimation();
                doubleAnimation.Duration = new Duration(new TimeSpan(0, 0, 0, 3, 0));
                Storyboard storyBoard = new Storyboard();
                storyBoard.Children.Add(doubleAnimation);
                Storyboard.SetTarget(doubleAnimation, txtLoginID);
                Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(FrameworkElement.WidthProperty));
                doubleAnimation.To = 500;
                storyBoard.Begin();

                txtLoginID.Text = "";
            }
        }

我试着将前景色改为透明,但文本仍然存在。
我也尝试了它与标签之前的文本框,它的工作很好,但我不能点击通过标签。你知道吗?

vwhgwdsa

vwhgwdsa1#

要淡出TextBox.Text,您应该设置TextBox.Foreground笔刷的Brush.Opacity属性的动画。

Storyboard.SetTarget(doubleAnimation, myTextBox);
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Foreground.Opacity"));

如果您的目标是创建水印TextBox,当TextBox接收键盘焦点时占位符消失,则应覆盖TextBoxControlTemplate
只需在文本主机(模板内部)上覆盖一个TextBlock,它包含占位符文本。然后为TextBlock.Opacity属性设置动画以获得淡入/淡出效果:

<TextBox>
  <TextBox.Style>
    <Style TargetType="TextBox">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="TextBox">
            <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
              <Grid>

                <!-- Mandatory presenter for the Text property -->
                <ScrollViewer x:Name="PART_ContentHost" />

                <!-- The overlay placeholder text presenter -->
                <TextBlock x:Name="WatermarkHost"
                           Text="deineMail@mail.de"
                           IsHitTestVisible="False"
                           Opacity="0" />
              </Grid>
            </Border>
            <ControlTemplate.Triggers>
              <MultiTrigger>
                <MultiTrigger.Conditions>
                  <Condition Property="Text"
                             Value="" />
                  <Condition Property="IsKeyboardFocusWithin"
                             Value="False" />
                </MultiTrigger.Conditions>

                <MultiTrigger.EnterActions>
                  <BeginStoryboard>
                    <Storyboard>
                      <DoubleAnimation Storyboard.TargetName="WatermarkHost"
                                       Storyboard.TargetProperty="Opacity"
                                       To="1"
                                       Duration="0:0:2" />
                    </Storyboard>
                  </BeginStoryboard>
                </MultiTrigger.EnterActions>
                <MultiTrigger.ExitActions>
                  <BeginStoryboard>
                    <Storyboard>
                      <DoubleAnimation Storyboard.TargetName="WatermarkHost"
                                       Storyboard.TargetProperty="Opacity"
                                       To="0"
                                       Duration="0:0:2" />
                    </Storyboard>
                  </BeginStoryboard>
                </MultiTrigger.ExitActions>
              </MultiTrigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </TextBox.Style>
</TextBox>

相关问题