我想在我的WPF项目中切换控件(Button,TextBox,Panel等)的不透明度,并想检查我是否已正确完成它。
我的问题是:这是您通常在XAML中编写的功能类型,还是使用类似下面的代码来实现淡入/淡出结果?
internal static class AnimationExtensions
{
internal enum TransitionSpeed
{
Instant = 0,
Fast = 100,
Normal = 200,
Slow = 500
}
/// <summary>
/// Toggles the opacity of a control.
/// </summary>
/// <param name="control">The control.</param>
internal static void ToggleControlFade(this Control control)
{
control.ToggleControlFade(TransitionSpeed.Normal);
}
/// <summary>
/// Toggles the opacity of a control.
/// </summary>
/// <param name="control">The control.</param>
/// <param name="speed">The speed.</param>
internal static void ToggleControlFade(this Control control, TransitionSpeed speed)
{
Storyboard storyboard = new Storyboard();
TimeSpan duration = new TimeSpan(0, 0, 0, 0, (int)speed); //
DoubleAnimation animation = new DoubleAnimation { From = 1.0, To = 0.0, Duration = new Duration(duration) };
if (control.Opacity == 0.0)
{
animation = new DoubleAnimation { From = 0.0, To = 1.0, Duration = new Duration(duration) };
}
Storyboard.SetTargetName(animation, control.Name);
Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity", 0));
storyboard.Children.Add(animation);
storyboard.Begin(control);
}
}
你可能会说我对WPF非常非常新。
由于
答案 0 :(得分:1)
我倾向于在动画之后需要执行动作的地方或者动画依赖于复杂触发器的地方找到代码隐藏是最佳位置,否则XAML是放置动画的好地方。 (我通常会为过渡或简单的'onclick'等事件做这件事。