You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.2 KiB

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace SJ.Controls
{
[StyleTypedProperty(Property = "Style", StyleTargetType = typeof(BTextBoxAnimation))]
public class BTextBoxAnimation : BTextBox
{
public static readonly DependencyProperty WaterRemarkTopStateColorProperty = DependencyProperty.Register("WaterRemarkTopStateColor", typeof(Brush), typeof(BTextBoxAnimation));
public static readonly DependencyProperty WaterRemarkStateProperty = DependencyProperty.Register("WaterRemarkState", typeof(WaterRemarkState), typeof(BTextBoxAnimation), new PropertyMetadata(WaterRemarkState.Normal, new PropertyChangedCallback((d, e) =>
{
if (e.OldValue != e.NewValue)
{
(d as BTextBoxAnimation).PlayWaterRemarkAnimation();
}
})));
private TextBlock txtRemark;
private TimeSpan animationTimeSpan = new TimeSpan(0, 0, 0, 0, 200);
private IEasingFunction animationEasingFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut };
public Brush WaterRemarkTopStateColor
{
get { return GetValue(WaterRemarkTopStateColorProperty) as Brush; }
set { SetValue(WaterRemarkTopStateColorProperty, value); }
}
public WaterRemarkState WaterRemarkState
{
get { return (WaterRemarkState)Convert.ToInt32(GetValue(WaterRemarkStateProperty)); }
set { SetValue(WaterRemarkStateProperty, value); }
}
static BTextBoxAnimation()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(BTextBoxAnimation), new FrameworkPropertyMetadata(typeof(BTextBoxAnimation)));
}
public BTextBoxAnimation()
{
this.Loaded += QLTextBoxAnimation_Loaded;
}
public override void OnApplyTemplate()
{
txtRemark = GetTemplateChild("txtRemark") as TextBlock;
base.OnApplyTemplate();
}
private void QLTextBoxAnimation_Loaded(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(Text))
WaterRemarkState = WaterRemarkState.Top;
}
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
if (!this.IsLoaded)
return;
if (string.IsNullOrEmpty(Text))
WaterRemarkState = WaterRemarkState.Normal;
else
WaterRemarkState = WaterRemarkState.Top;
}
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
WaterRemarkState = WaterRemarkState.Top;
}
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
if (string.IsNullOrEmpty(Text))
WaterRemarkState = WaterRemarkState.Normal;
}
private void PlayWaterRemarkAnimation()
{
var fontsize = WaterRemarkState == WaterRemarkState.Normal ? FontSize : 10.5;
var row = WaterRemarkState == WaterRemarkState.Normal ? 1 : 0;
var storyboard = new Storyboard();
var daukf_Remark_FontSize = new DoubleAnimationUsingKeyFrames();
daukf_Remark_FontSize.KeyFrames.Add(new EasingDoubleKeyFrame(fontsize, animationTimeSpan, animationEasingFunction));
Storyboard.SetTargetProperty(daukf_Remark_FontSize, new PropertyPath("(TextBlock.FontSize)"));
Storyboard.SetTarget(daukf_Remark_FontSize, txtRemark);
storyboard.Children.Add(daukf_Remark_FontSize);
var i32aukf_Remark_Row = new Int32AnimationUsingKeyFrames();
i32aukf_Remark_Row.KeyFrames.Add(new EasingInt32KeyFrame(row, animationTimeSpan, animationEasingFunction));
Storyboard.SetTargetProperty(i32aukf_Remark_Row, new PropertyPath("(Grid.Row)"));
Storyboard.SetTarget(i32aukf_Remark_Row, txtRemark);
storyboard.Children.Add(i32aukf_Remark_Row);
storyboard.Begin();
}
}
public enum WaterRemarkState
{
Normal, Top
}
}