我正在擺弄水印文本框。我創建了一個樣式,在模板中我有一個 TextBlock,它的 Text 屬性系結到 Tag,當 TextBox 獲得焦點時,我用影片將水印向上推。我已經設法在 XAML 中實作了這種行為(它作業得很好),下面是代碼:
<Style x:Key="WatermarkTextBox" TargetType="{x:Type TextBox}">
<!-- Generic styling -->
<Setter Property="BorderThickness" Value="1 1 1 1" />
<Setter Property="BorderBrush" Value="#ced4da" />
<Setter Property="Padding" Value="6 6 6 6" />
<Setter Property="FontSize" Value="16" />
<!-- Template -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<!-- Textbox area -->
<Border
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
CornerRadius="6"
Padding="{TemplateBinding Padding}"
>
<ScrollViewer
x:Name="PART_ContentHost"
VerticalAlignment="Center"
/>
</Border>
<!-- Watermark -->
<Border
x:Name="watermark"
Padding="4 0 4 0"
Background="#ffffff"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="8 0 0 0"
>
<Border.RenderTransform>
<TranslateTransform X="0" Y="0" />
</Border.RenderTransform>
<!-- Watermark text -->
<TextBlock
Text="{TemplateBinding Tag}"
Foreground="#212529"
FontSize="17"
/>
</Border>
</Grid>
<!-- Triggers -->
<ControlTemplate.Triggers>
<!-- Watermark animation -->
<Trigger Property="IsFocused" Value="True">
<!-- Push watermark on top -->
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="watermark"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)"
From="0"
To="-24"
Duration="0:0:0.1"
/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<!-- Return watermark to original position -->
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="watermark"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)"
From="-24"
To="0"
Duration="0:0:0"
/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
但是,我也想從后面的代碼中實作這個影片。我嘗試在“GotFocus”事件上使用以下代碼,但影片沒有播放。
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox txb = sender as TextBox;
Border watermark = (Border)txb.Template.FindName("watermark", txb);
DoubleAnimation moveAnimation = new DoubleAnimation(0, -24, new Duration(TimeSpan.FromSeconds(0.1)));
watermark.BeginAnimation(TranslateTransform.YProperty, moveAnimation);
}
如何將上述影片從 XAML 轉換為后面的代碼?
uj5u.com熱心網友回復:
您的影片目標顯然是錯誤的。Border沒有TranslateTransform.YProperty財產。
您必須選擇正確的影片目標,在您的情況下TranslateTransform是Border.
我建議擴展TextBox以封裝事件處理并添加Watermark屬性以更方便。如果將 typeobject和 host 的屬性設為 a ContentPresenter(或ContentControl),您可以支持任何內容作為占位符,例如Image.
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox txb = sender as TextBox;
Border watermark = (Border)txb.Template.FindName("watermark", txb);
var animationTarget = (TranslateTransform)watermark.RenderTransform;
var moveAnimation = new DoubleAnimation(0, -24, new Duration(TimeSpan.FromSeconds(0.1)));
animationTarget.BeginAnimation(TranslateTransform.YProperty, moveAnimation);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490833.html
