WPF中自动增加行(动画)的TextBox
原文:WPF中自动增加行(动画)的TextBox

WPF中自动增加行(动画)的TextBox

WPF中的Textbox控件是可以自动换行的,只要设置TextWrapping属性为”Wrap”即可,但是存在一个问题:Textbox的高度是固定的,当输入文本过多时就会出现如下情况。

Textbox虽然没有自动增加高度的属性,但是我们可以通过设置来实现这一个功能。相关xaml代码如下

 <Grid VerticalAlignment="Top" HorizontalAlignment="Left" Width="36" Height="100"><TextBox x:Name="textBox" TextWrapping="Wrap" VerticalAlignment="Top" /></Grid>

效果如下

这里需要注意的是

  • 如果设置了textBox的Width、Height或者Margin属性,那么此textBox的大小就已经限制死了,不会自动增加,会出现上图中的情况。
  • 如果想设置textBox的大小和位置,需要把textBox放在一个Grid中,通过Grid的属性来控制textBox(上文中xaml文件中的方法)

其实到此为止所需要的功能已经实现了,但是为了让让textbox更美观,我加了一个动画

效果如下:

表面上看是一个textbox,其实是一个Grid加上2个textBox(一个显示,一个隐藏),xaml代码如下

<Grid><TextBox x:Name="txtVisible" TextWrapping="Wrap"/><TextBox x:Name="txtHidden" TextWrapping="Wrap" Visibility="Hidden" VerticalAlignment="Top"/>
</Grid>

Grid是用来控制textbox的大小的,两个textbox中,显示的那个用于输入文字,隐藏的用于触发动画。原理如下
在txtVisible中输入文字的同时,txtVisible把text传递给txtHidden,当txtHidden中的内容已经满一行时,会触发SizeChange事件,这个事件再触发txtVisible高度变化动画。代码如下

public partial class txt : UserControl
{private HeightAnimation anim;private double _animationDuration;public txt(){InitializeComponent();// Initialize the animationanim = new HeightAnimation(this);AnimationDuration = 500; //default value// Add the handlers to the required eventstxtHidden.SizeChanged += TxtHidden_SizeChanged;txtVisible.TextChanged += TxtVisible_TextChanged;}/// <summary>/// Gets or sets a value indicating whether the control is animated on loaded./// </summary>public bool AnimateOnLoaded { get; set; } = false;/// <summary>/// Gets or sets a value indicating whether the control is animated./// </summary>public bool IsAnimated { get; set; } = true;/// <summary>/// Gets or sets the duration of the animation./// </summary>public double AnimationDuration{get { return _animationDuration; }set{_animationDuration = value;anim.Duration = new Duration(TimeSpan.FromMilliseconds(value));}}/// <summary>/// Gets or sets the text contents of the AnimatedTextBox./// </summary>public string Text{get { return txtHidden.Text; }set{txtHidden.Text = value;txtVisible.Text = value;}}private void TxtVisible_TextChanged(object sender, TextChangedEventArgs e){// When the user's writing in txtVisible, we copy the text to txtHiddentxtHidden.Text = txtVisible.Text;}private void TxtHidden_SizeChanged(object sender, SizeChangedEventArgs e){OnHeightChanged(e.PreviousSize.Height, e.NewSize.Height);}/// <summary>/// To execute when the txtHidden's Height has changed./// </summary>private void OnHeightChanged(double previousHeight, double newHeight){//Animation type, increase txtVisible's height or decreaseanim.ChangeType = (newHeight > previousHeight) ? HeightAnimation.ChangeTypes.Increased : HeightAnimation.ChangeTypes.Decreased;// Animate the Height from the txtHidden's previousHeight to its newHeightanim.From = previousHeight;anim.To = newHeight;// Start the animationanim.BeginAnimation();}/// <summary>/// Manages the AnimatedTextBox Height's animation./// </summary>private class HeightAnimation{private Storyboard sb;private DoubleAnimation anim;private double _from;private double _to;private Duration _duration;private FrameworkElement _fe;private ChangeTypes _changeType;/// <summary>/// The possible types of the Height change./// </summary>public enum ChangeTypes{Increased,Decreased}/// <summary>/// Constructor of the class./// </summary>public HeightAnimation(FrameworkElement fe){// Set the FrameworkElement which manages the animation_fe = fe;// Initialize the Storyboardsb = new Storyboard();sb.AutoReverse = false;// Initialize the animationanim = new DoubleAnimation();anim.Name = "anim";// Set the EasingFunction on a new instance of CubicEase whose EasingMode is EaseInOutanim.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseInOut };// Bind the Animation with the txtVisible TextBoxStoryboard.SetTargetName(anim, "txtVisible");// Add the animation to the Storyboard's childrensb.Children.Add(anim);}/// <summary>/// Gets or sets the type of the Height change./// </summary>public ChangeTypes ChangeType{get { return _changeType; }set{_changeType = value;/* If the Height has inreased, set the target property to MaxHeight, else to MinHeight* (instead of animating directly the Height, we animate MaxHeight/MinHeight to prevent the AnimatedTextBox* from growing/shrinking suddenly) */Storyboard.SetTargetProperty(anim, new PropertyPath(string.Format("(TextBox.{0})", (value == ChangeTypes.Increased) ? "MaxHeight" : "MinHeight")));}}/// <summary>/// Gets or sets the animation's starting Height./// </summary>public double From{get { return _from; }set{_from = value;anim.From = value;}}/// <summary>/// Gets or sets the animation's ending Height./// </summary>public double To{get { return _to; }set{_to = value;anim.To = value;}}/// <summary>/// Gets or sets the animation's duration./// </summary>public Duration Duration{get { return _duration; }set{_duration = value;anim.Duration = value;}}/// <summary>/// Begins the animation./// </summary>public void BeginAnimation(){_fe.BeginStoryboard(sb);}}
}

posted on 2019-01-15 17:08 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10272989.html

WPF中自动增加行(动画)的TextBox相关推荐

  1. java行数可变的表格,怎么在表格中自动增加行,并对输入的数据作判断

    当前位置:我的异常网» Java Web开发 » 怎么在表格中自动增加行,并对输入的数据作判断 怎么在表格中自动增加行,并对输入的数据作判断 www.myexceptions.net  网友分享于:2 ...

  2. 【转】禁用Chrome和Firefox中自动播放的动画GIF

    动画GIF已经腐臭,并且在浏览时自动播放时可能会非常烦人.以下是如何阻止他们在Chrome和Firefox中自动播放. 禁用动画GIF Firefox 在地址栏中输入:  about:config,然 ...

  3. html 选择自动增加行数,为网页中的源代码自动设置行号

    本文演示了如何为网页中的源代码块自动设置行号. 现在,我们准备在网页中显示C++的Hello world.这段著名的代码如下所示: #include intmain() { std::cout < ...

  4. html标签table自动增加行,给表格添加行的标签

    表格标签如何添加一个新的? 加了个新数据,但是标签一直不知道怎么添加1个和其他一样的 从Windows的"开始"菜单里,找到excle2010,或者右键新建一个excel文档. 单 ...

  5. 关于datagridview自动增加行高度和显示全部内容的设置

    this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;  //自动调动dat ...

  6. html 选择自动增加行数,HTML怎么自动计算出上两行的值在第三行里面?

    如果使用原生js的话,需要做的工作就稍微有点多了.一步一步来吧. 第一点:首先要明确需要绑定什么事件.实时监听的话,给input绑定change事件是不行的.绑定的change事件如果我没记错的话需要 ...

  7. gridControl自动增加行添加数据

    绑定gridControl的对象list,(opList为ord_plantofo_view的list:) gridControl1.DataSource = new BindingList<o ...

  8. C# datagridview、datagrid、GridControl增加行号

    01 - WinForm中datagridview增加行号 在界面上拖一个控件dataGridView1,在datagridview添加行事件中添加如下代码: private void dataGri ...

  9. CleanAOP实战系列--WPF中MVVM自动更新

    CleanAOP实战系列--WPF中MVVM自动更新 作者: 立地 邮箱: jarvin_g@126.com QQ: 511363759 CleanAOP介绍:https://github.com/J ...

最新文章

  1. android之json解析优化,Android开发之json解析
  2. MLPclassifier,MLP 多层感知器的的缩写(Multi-layer Perceptron)
  3. CSS3---4.伪元素选择器
  4. mysql确认半同步命令_怎么判断mysql是否是半同步复制
  5. ITIL V3 服务转换篇 之 服务转换阶段的组织形式
  6. mybatis 鉴别其_MyBatis之Mapper XML 文件详解(四)-JDBC 类型和嵌套查询
  7. MacFree ePlicy Orchestrator
  8. python字典练习_python字典练习
  9. linux运维和python运维_Linux运维对比:Shell运维 VS Python运维
  10. VMware Workstation 虚拟机中的 虚拟交换机和地址规划
  11. qpsk调制matlab实现,QPSK调制与解调系统的MATLAB实现
  12. 三星s10刷android原生,【极光ROM】-【三星S10E/S10/S10+ G97XX-9820】-【V12.0 Android-Q-TD1】...
  13. matlab cy68013,基于MATLAB的码垛机械手运动学分析与仿真
  14. MATLAB平台文字识别算法实现
  15. 大学生面试:教你用“一切细节”征服HR
  16. elasticsearch 修改已存在的filed值 和 新增filed
  17. SpringAMQP-Basic Queue、Work Queue、Fanout、Direct、Topic
  18. 02-JavaScript变量的强制类型转换
  19. 2019中国机器人大赛窄足机器人赛后总结
  20. 1053:最大数输出

热门文章

  1. STM32F4 HAL 库开发报错:Hal_StatusTypeDef is undefined的解决办法
  2. 哈希存储 java_Java容器系列之HashMap的存储
  3. TQ210 —— S5PV210 gboot设计
  4. LeetCode 2079. 给植物浇水(前缀和)
  5. LeetCode 1702. 修改后的最大二进制字符串(贪心)
  6. LeetCode 1071. 字符串的最大公因子(字符串的最大公约数)
  7. 利用Python爬取网易上证所有股票数据(代码
  8. python中的__new__概念(工厂
  9. iphone版 天行skyline_Skyline QT
  10. python3.6是用来干嘛的_学 Python 都用来干嘛的?