博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【WPF系列】Textbox
阅读量:4363 次
发布时间:2019-06-07

本文共 4588 字,大约阅读时间需要 15 分钟。

Style定义实例

给Textbox定义一个阴影效果。

 

UpdateSourceTrigger

 

默认UpdateSourceTrigger为LostFoucs,有时需要及时CommitValue,则需要设置为PropertyChanged。这样当Text属性的值发生变化时,我们的值就能及时更新到Datasource中。

更多参考

NumberTextbox

  1. 使用NubmberTextboxBehavior
  2. 将TextBox的binding属性中Delay设置为1000
//NumericTextBoxBehavior///     /// This forces a TextBoxBase control to be numeric-entry only    ///     /// 
/// ]]> ///
public static class NumericTextBoxBehavior { #region IsEnabled DP /// /// Dependency Property for turning on numeric behavior in a TextBox. /// public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(NumericTextBoxBehavior), new UIPropertyMetadata(false, OnEnabledStateChanged)); /// /// Attached Property getter for the IsEnabled property. /// /// Dependency Object ///
Current property value
public static bool GetIsEnabled(DependencyObject source) { return (bool)source.GetValue(IsEnabledProperty); } /// /// Attached Property setter for the IsEnabled property. /// /// Dependency Object /// Value to set on the object public static void SetIsEnabled(DependencyObject source, bool value) { source.SetValue(IsEnabledProperty, value); } /// /// This is the property changed handler for the IsEnabled property. /// /// /// private static void OnEnabledStateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { TextBox tb = sender as TextBox; if (tb == null) return; tb.PreviewTextInput -= tbb_PreviewTextInput; DataObject.RemovePastingHandler(tb, OnClipboardPaste); bool b = ((e.NewValue != null && e.NewValue.GetType() == typeof(bool))) ? (bool)e.NewValue : false; if (b) { tb.PreviewTextInput += tbb_PreviewTextInput; DataObject.AddPastingHandler(tb, OnClipboardPaste); } } #endregion #region Private Methods /// /// This method handles paste and drag/drop events /// onto the TextBox. It restricts the character /// set to numerics and ensures we have consistent behavior. /// /// TextBox sender /// EventArgs private static void OnClipboardPaste(object sender, DataObjectPastingEventArgs e) { TextBox tb = sender as TextBox; string text = e.SourceDataObject.GetData(e.FormatToApply) as string; if (tb != null && !string.IsNullOrEmpty(text) && !Validate(tb, text)) e.CancelCommand(); } /// /// This checks if the resulting string will match the regex expression /// static void tbb_PreviewTextInput(object sender, TextCompositionEventArgs e) { TextBox tb = sender as TextBox; if (tb != null && !Validate(tb, e.Text)) e.Handled = true; } #endregion private static bool Validate(TextBox tb, string newContent) { string testString = string.Empty; // replace selection with new text. if (!string.IsNullOrEmpty(tb.SelectedText)) { string pre = tb.Text.Substring(0, tb.SelectionStart); string after = tb.Text.Substring(tb.SelectionStart + tb.SelectionLength, tb.Text.Length - (tb.SelectionStart + tb.SelectionLength)); testString = pre + newContent + after; } else { string pre = tb.Text.Substring(0, tb.CaretIndex); string after = tb.Text.Substring(tb.CaretIndex, tb.Text.Length - tb.CaretIndex); testString = pre + newContent + after; } Regex regExpr = new Regex(@"^([-+]?)(\d*)([,.]?)(\d*)$"); if (regExpr.IsMatch(testString)) return true; return false; } }

WaterMark/HintText/PlaceHoder

  1. 通过Style
  2. 通过AttachBehavior
  3. 通过自定义控件

1.给TextBox添加水印效果(提示文字)

2.通过AttachBehavior

 

3.通过自定义控件

 

 

参考

转载于:https://www.cnblogs.com/HQFZ/p/4313001.html

你可能感兴趣的文章
Mysql中代替like模糊查询的一种方法
查看>>
C++实例讲解Binder通信
查看>>
AutoCAD如何方便截图放到Word文档,改成白底黑字
查看>>
Django 和 html
查看>>
算法与数据结构(一)
查看>>
【java】对象变成垃圾被垃圾回收器gc收回前执行的操作:Object类的protected void finalize() throws Throwable...
查看>>
数据库建表练习(10.11作业)
查看>>
如何配置能让fiddler抓去https的请求?
查看>>
SpringBoot 2.0 更优雅的配置注入
查看>>
[慢查优化]联表查询注意谁是驱动表 & 你搞不清楚谁join谁更好时请放手让mysql自行判定...
查看>>
liunx之Centos6.8杀毒软件的安装
查看>>
充实的日子里忙忙碌碌
查看>>
十三、oracle 数据字典和动态性能视图
查看>>
插件开发-UI插件开发
查看>>
[转] vim自定义配置 和 在ubnetu中安装vim
查看>>
Windows环境下安装、卸载Apache
查看>>
HTTPS协议在Tomcat中启用的配置
查看>>
Collections.sort的使用
查看>>
圆形坠落模拟算法设计
查看>>
vi @-function
查看>>