Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在文本框中写入文本的数据验证,MVVM C#_C#_Wpf_Mvvm_Validation - Fatal编程技术网

在文本框中写入文本的数据验证,MVVM C#

在文本框中写入文本的数据验证,MVVM C#,c#,wpf,mvvm,validation,C#,Wpf,Mvvm,Validation,我正在制作一个应用程序,我喜欢将一些字节写入文本框。我喜欢验证是否将真正的十六进制代码写入文本框,如果没有,则提醒用户 我从来没有在MVVM和XAML中做过这个。怎么做?我在网上找到了一些教程,但问题是我喜欢写64字节。我有64个文本框聚集在一个数组中 其中一个文本框: <TextBox Text="{Binding TB[4], UpdateSourceTrigger=PropertyChanged}" Grid.Column="0" Grid.Row="0" Style="{Stati

我正在制作一个应用程序,我喜欢将一些字节写入文本框。我喜欢验证是否将真正的十六进制代码写入文本框,如果没有,则提醒用户

我从来没有在MVVM和XAML中做过这个。怎么做?我在网上找到了一些教程,但问题是我喜欢写64字节。我有64个文本框聚集在一个数组中

其中一个文本框:

<TextBox Text="{Binding TB[4], UpdateSourceTrigger=PropertyChanged}" Grid.Column="0" Grid.Row="0" Style="{StaticResource byteTextBoxStyle}"/>
目标是使红色文本块位于所有文本框的下方,并编写一个红色()

我可以在按下按钮后再做——将数组拉到一个字符串中,然后用正则表达式检查是否有问题但当用户输入文本并立即识别是否正常时,我希望实时执行此操作


请帮忙,因为我是MVVM和WPF方面的新手。如果有任何问题,请提问。谢谢

我过去使用System.Windows.Interactivity.dll做过类似的事情

如果输入了非十六进制值,它所做的就是终止键关闭事件

{

/// <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);
        }
    }

}
{
/// 
///提供允许用户仅键入字母[0-9 A-F A-F]的功能。
/// 
公共类HexEditTextBox:TriggerAction
{
受保护的覆盖无效调用(对象参数)
{
var textBox=this.AssociatedObject as textBox;
如果(textBox!=null)textBox.PreviewKeyDown+=handlepreviewkeydown事件;
}
/// 
///检查输入是否为十六进制数的有效键。
///如果输入无效,则将“Handled”属性设置为True,以便不会对此操作执行进一步的操作。
/// 
/// 
///KeyEventArgs实例
私有void HandlePreviewKeyDownEvent(对象发送方,KeyEventArgs e)
{
var acceptedKeys=新列表()
{
键D0,键D1,键D2,键D3,键D4,键D5,键D6,键D7,键D8,键D9,
键A,键B,键C,键D,键E,键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);
}
}
}
您应该能够在此处插入验证

{

/// <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);
        }
    }

}