Style定义实例
给Textbox定义一个阴影效果。
UpdateSourceTrigger
默认UpdateSourceTrigger为LostFoucs,有时需要及时CommitValue,则需要设置为PropertyChanged。这样当Text属性的值发生变化时,我们的值就能及时更新到Datasource中。
更多参考
NumberTextbox
- 使用NubmberTextboxBehavior
- 将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
- 通过Style
- 通过AttachBehavior
- 通过自定义控件
1.给TextBox添加水印效果(提示文字)
2.通过AttachBehavior
3.通过自定义控件