实在搞不懂,自定义控件中动画问题,求助~!

蓝色_冰点 蓝色_冰点 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
自己学着做了个自定义的控件,功能很简单,就是一个文本框,如果输入内容是数字,文本颜色为蓝
如果不是数字,文本颜色为红
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:local="clr-namespace:SLClient" >

<!--The default style of MyCtrl-->
<Style TargetType="local:MyCtrl">
<!--设置默认样式-->
<Setter Property="Width" Value="100"/>

<!--在该样式中,设置其模板内容-->
<Setter Property="Template">
<Setter.Value>
<!-- 定义模板 -->
<ControlTemplate TargetType="local:MyCtrl">
<!--该模板中,应只有一个可视元素,在此用Grid-->
<Grid>
<!--定义可视状态组-->
<vsm:VisualStateManager.VisualStateGroups>
<!-- 数据有效性状态组 -->
<VisualStateGroup x:Name="ValidStates">
<VisualState x:Name="Valid" />
<VisualState x:Name="UnValid" />
<!--定义状态转换动作-->
<VisualStateGroup.Transitions>
<!--当跳转至该状态时,进行转换-->
<VisualTransition To="UnValid">
<!--时间轴-->
<Storyboard>
<!--动画设定.颜色渐变-->
<ColorAnimation Storyboard.TargetName="textBox"
Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)"
To="Red"/>
</Storyboard>
</VisualTransition>
<VisualTransition To="Valid">
<!--时间轴-->
<Storyboard>
<!--动画设定.颜色渐变-->
<ColorAnimation Storyboard.TargetName="textBox"
Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)"
To="Blue"/>
</Storyboard>
</VisualTransition>
</VisualStateGroup.Transitions>
</VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<!--文本框-->
<TextBox Name="textBox" Margin="0,0,0,0" Grid.Column="0" Grid.Row="0"></TextBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SLClient
{
//定义该类的部件和状态
[TemplatePart(Name = "textBox", Type = typeof(TextBox))]
[TemplateVisualState(Name = "Valid", GroupName = "ValidStates")]
[TemplateVisualState(Name = "UnValid", GroupName = "ValidStates")]
//我知道实现这样的功能,直接扩展TextBox更方便
//之所以这样做,只是想练习一下怎么做一个自定义控件罢了
//所以别告诉我应该扩展自TextBox,而不是 Control
public class MyCtrl : Control
{
public MyCtrl()
{
//设置默认样式
this.DefaultStyleKey = typeof(MyCtrl);
}

//重载父类方法,当对该控件应用模板时该方法执行
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
//获取模板中的部件
this.TextBox = this.GetTemplateChild("textBox") as TextBox;
}

private TextBox textBox;
protected TextBox TextBox
{
get { return this.textBox; }
set
{
if (this.textBox != null)
{//取消事件注册
this.textBox.TextChanged -= new TextChangedEventHandler(textBox_TextChanged);

}
this.textBox = value;
if (this.textBox != null)
{//部件事件注册
this.textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
}
}
}

void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
//数据有效性的判断
decimal num;
bool valid = Decimal.TryParse(this.textBox.Text, out num);
if (valid)
{
VisualStateManager.GoToState(this, "Valid", true);
}
else
{
VisualStateManager.GoToState(this, "UnValid", true);
}
}
}
}

代码如上。OK,编译通过,运行能正常判断用户输入,而且也能变色。问题是。。。。。。
文本初始颜色是黑色,当颜色发生变化结束的时候,总是突然跳变到原来的黑色

求助解决办法。。。。。
...全文

相关推荐

LabVIEW自定义布尔控件合集(~1000种).rar

LabVIEW自定义布尔控件合集,近千种好看全面的自定义控件,多种风格满足您的需要,同一种风格格式统一,踩过无数坑收集来的,这么多年一直在用,绝对值得您下载,如果好用,麻烦分享~~~

bsfz0000

Delphi老程序界面现代化:SmartEffects VCL特效控件安装与实战

在Windows桌面应用开发中,界面视觉效果直接影响用户体验。对于基于Delphi VCL构建的传统Win32应用而言,不重写框架即可实现现代化UI升级,一直是开发者的核心诉求。VCL特效组件通过拦截绘制过程、叠加阴影与动画层,在既有控件机制上提供轻量级增强,相比自绘UI大幅降低了开发与维护成本。本文以Almediadev SmartEffects VCL 3.61为例,围绕窗体阴影、按钮动效、登录窗口等典型场景,系统讲解控件包安装流程、设计期与运行期包配置、属性调优技巧及常见兼容性问题,并给出与老项目融合

weixin_30908103

Qt自定义控件动画效果按钮

Qt自定义控件动画效果按钮

草莓蛋糕人

可动画收缩展开的自定义控件

适用于wpf的vs2010自定义控件,实现了点击按钮可以展开或收缩内容的功能,面板的内容和导航条的内容都可自定义,含完整源码和示例。 BroadSidePanel的主要属性如下: FrontContent折叠展开的内容 ExpandButtonContent导航按钮的内容 ExpandButtonDockPosition导航按钮的停靠位置:上中下 CornerRadius折叠展开面板的圆角 IsExpanded是展开还是收缩状态,给该属性赋值可以实现动态控制展开和收缩 ExpandButtonWidth导航栏的宽度,默认20

new_coming

自定义控件+帧动画

在Android前端开发的时候经常会用到自定义控件,现在很多控件都要用到圆角或是圆形,源码中详细注释了自定义控件

酱子懒
评论 6
收藏 已收藏
分享
返回顶部
蓝色_冰点
WPF/Silverlight 8,757 成员 12,031 内容