esispaned
esispaned

Reputation: 303

Data Validation of Text written in TextBox, MVVM C#

I making an app, where I like writte some bytes into textbox. I like to validate if real HEX code is written into textbox or not and remind user if not.

I never made this in MVVM and XAML. How to do it? I find several tutorial on web, but problem is that I like to write 64 bytes. I have 64 textboxes pull together in one array.

One of the textbox:

<TextBox Text="{Binding TB[4], UpdateSourceTrigger=PropertyChanged}" Grid.Column="0" Grid.Row="0" Style="{StaticResource byteTextBoxStyle}"/>

and array variable:

private string[] _tb = new string[64];
    public string[] TB
    {
        get
        { return _tb; }
        set
        {
            _tb = value;

            NotifyPropertyChanged("TB");
        }
    }

Goal is that red textblock is under of all textboxes and write a red (Something like that).

I can do it later when button is pressed - pull together array in to one string and check with regex is something is not OK. But I want this in real time, when user put-in text and right away recognite if is OK or not.

Please for help, because I am new in MVVM and WPF thing. If any question please ask. Thanks!

Upvotes: 3

Views: 568

Answers (1)

GWT
GWT

Reputation: 33

I have done something similar in the past using System.Windows.Interactivity.dll

https://www.nuget.org/packages/System.Windows.Interactivity.WPF/

All it does is terminate the key down event if a non hex value is keyed in.

{

/// <summary>
    /// Provides functionality to allow users to type only letters [0-9 A-F a-f]. 
    /// </summary>
    public class HexEditTextBox : TriggerAction<DependencyObject>
    {
        protected override void Invoke(object parameter)
        {
            var textBox = this.AssociatedObject as TextBox;
            if (textBox != null) textBox.PreviewKeyDown += HandlePreviewKeyDownEvent;
        }

        /// <summary>
        /// Checks whether the input is a valid key for a Hex number.
        /// Sets the 'Handled' Property as True if the input is invalid, so that further actions will not be performed for this Action.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">KeyEventArgs instance</param>
        private void HandlePreviewKeyDownEvent(object sender, KeyEventArgs e)
        {
            var acceptedKeys = new List<Key>()
                                         {
                                             Key.D0, Key.D1, Key.D2, Key.D3,Key.D4,Key.D5,Key.D6,Key.D7,Key.D8,Key.D9,
                                             Key.A,Key.B,Key.C,Key.D,Key.E,Key.F,
                                             Key.Tab,Key.Back,Key.Delete,Key.Left,Key.Right,Key.Up,Key.Down,Key.Enter,Key.Home,Key.End,
                                             Key.NumPad0,Key.NumPad1,Key.NumPad2,Key.NumPad3,Key.NumPad4,Key.NumPad5,Key.NumPad6,Key.NumPad7,Key.NumPad8,Key.NumPad9
                                         };

            e.Handled = !acceptedKeys.Contains(e.Key);
        }
    }

}

You should be able to insert your validation here.

Upvotes: 2

Related Questions