Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can restrict the input to numbers only using an attached property on the TextBox. Define the attached property once (even in a separate dll) and use it on any TextBox. Here is the attached property:</p> <pre><code> using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; /// &lt;summary&gt; /// Class that provides the TextBox attached property /// &lt;/summary&gt; public static class TextBoxService { /// &lt;summary&gt; /// TextBox Attached Dependency Property /// &lt;/summary&gt; public static readonly DependencyProperty IsNumericOnlyProperty = DependencyProperty.RegisterAttached( "IsNumericOnly", typeof(bool), typeof(TextBoxService), new UIPropertyMetadata(false, OnIsNumericOnlyChanged)); /// &lt;summary&gt; /// Gets the IsNumericOnly property. This dependency property indicates the text box only allows numeric or not. /// &lt;/summary&gt; /// &lt;param name="d"&gt;&lt;see cref="DependencyObject"/&gt; to get the property from&lt;/param&gt; /// &lt;returns&gt;The value of the StatusBarContent property&lt;/returns&gt; public static bool GetIsNumericOnly(DependencyObject d) { return (bool)d.GetValue(IsNumericOnlyProperty); } /// &lt;summary&gt; /// Sets the IsNumericOnly property. This dependency property indicates the text box only allows numeric or not. /// &lt;/summary&gt; /// &lt;param name="d"&gt;&lt;see cref="DependencyObject"/&gt; to set the property on&lt;/param&gt; /// &lt;param name="value"&gt;value of the property&lt;/param&gt; public static void SetIsNumericOnly(DependencyObject d, bool value) { d.SetValue(IsNumericOnlyProperty, value); } /// &lt;summary&gt; /// Handles changes to the IsNumericOnly property. /// &lt;/summary&gt; /// &lt;param name="d"&gt;&lt;see cref="DependencyObject"/&gt; that fired the event&lt;/param&gt; /// &lt;param name="e"&gt;A &lt;see cref="DependencyPropertyChangedEventArgs"/&gt; that contains the event data.&lt;/param&gt; private static void OnIsNumericOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { bool isNumericOnly = (bool)e.NewValue; TextBox textBox = (TextBox)d; if (isNumericOnly) { textBox.PreviewTextInput += BlockNonDigitCharacters; textBox.PreviewKeyDown += ReviewKeyDown; } else { textBox.PreviewTextInput -= BlockNonDigitCharacters; textBox.PreviewKeyDown -= ReviewKeyDown; } } /// &lt;summary&gt; /// Disallows non-digit character. /// &lt;/summary&gt; /// &lt;param name="sender"&gt;The source of the event.&lt;/param&gt; /// &lt;param name="e"&gt;An &lt;see cref="TextCompositionEventArgs"/&gt; that contains the event data.&lt;/param&gt; private static void BlockNonDigitCharacters(object sender, TextCompositionEventArgs e) { foreach (char ch in e.Text) { if (!Char.IsDigit(ch)) { e.Handled = true; } } } /// &lt;summary&gt; /// Disallows a space key. /// &lt;/summary&gt; /// &lt;param name="sender"&gt;The source of the event.&lt;/param&gt; /// &lt;param name="e"&gt;An &lt;see cref="KeyEventArgs"/&gt; that contains the event data.&lt;/param&gt; private static void ReviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Space) { // Disallow the space key, which doesn't raise a PreviewTextInput event. e.Handled = true; } } } </code></pre> <p>Here is how to use it (replace "controls" with your own namespace):</p> <pre><code>&lt;TextBox controls:TextBoxService.IsNumericOnly="True" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload