WPF文本框验证C# [英] WPF TextBox Validation C#

查看:482
本文介绍了WPF文本框验证C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个文本框(为Id1 名称薪酬)。 编号薪酬应该包含整数和名称应该只包含字符。我需要验证我的文本框,它应该显示错误,因为我进入错误的字符或整数。也可以这样XAML中唯一没有完成codebehind?我是新来WPF和验证,请帮我需要code

这是XAML中code:

 <文本框名称=TB1的Horizo​​ntalAlignment =左身高=20保证金=60,10,0,0TextWrapping =NoWrap的文本={结合SelectedItem.Id,的ElementName = dgsample}VerticalAlignment =评出的WIDTH =100/>
<文本框名称=TB2的Horizo​​ntalAlignment =左身高=20保证金=60,60,0,0TextWrapping =NoWrap的文本={结合SelectedItem.Name,的ElementName = dgsample}VerticalAlignment = 顶级WIDTH =100/>
<文本框名称=TB3的Horizo​​ntalAlignment =左身高=20保证金=60,110,0,0TextWrapping =NoWrap的文本={结合SelectedItem.Salary,的ElementName = dgsample}VerticalAlignment =壮志WIDTH =100/>


解决方案

您还可以实施 IDataErrorInfo的作为作为视图模型如下。如果实施 IDataErrorInfo的,你可以做的,与其特定的se​​tter方法​​的验证,那么只要有一个错误,返回一个错误信息,这样的文本框中其中有错误得到一个红色框周围,指示错误。

 类视图模型:INotifyPropertyChanged的,IDataErrorInfo的
{
    私人字符串m_Name =请在此处输入;
    公共视图模型()
    {
    }    公共字符串名称
    {
        得到
        {
            返回m_Name;
        }
        组
        {
            如果(m_Name!=值)
            {
                m_Name =价值;
                OnPropertyChanged(姓名);
            }
        }
    }    公共事件PropertyChangedEventHandler的PropertyChanged;    保护无效OnPropertyChanged(字符串propertyName的)
    {
        如果(的PropertyChanged!= NULL)
        {
            的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
        }
    }    公共字符串错误
    {
        {返回......; }
    }    ///<总结>
    当过它的值改为///将被调用为每一个物业
    ///< /总结>
    ///< PARAM NAME =COLUMNNAME>其值改为&LT的属性的名称; /参数>
    ///<&回报GT;< /回报>
    公共字符串此[字符串COLUMNNAME]
    {
        得到
        {
            返回验证(COLUMNNAME);
        }
    }    私人字符串验证(字符串propertyName的)
    {
        //返回的错误信息,如果有其他人上回空或空字符串错误
        字符串validationMessage =的String.Empty;
        开关(propertyName的)
        {
            案件的Name://属性名
                // TODO:检查validiation条件
                validationMessage =错误;
                打破;
        }        返回validationMessage;
    }
}

和您必须设置 ValidatesOnDataErrors = TRUE 在XAML以便调用 IDataErrorInfo的的方法如下

 <文本框的文本={绑定名称,UpdateSourceTrigger = PropertyChanged的,ValidatesOnDataErrors = TRUE}/>

I have 3 TextBoxes (Id1,Name and Salary). Id and Salary should contain integers and Name should only contain characters. I need validations for my TextBox, it should show errors as I enter wrong characters or integers. Also can this be done only in Xaml without codebehind? I'm new to Wpf and validation please help me with the required code

This is Xaml code:

<TextBox Name="tb1" HorizontalAlignment="Left" Height="20" Margin="60,10,0,0" TextWrapping="NoWrap" Text="{Binding SelectedItem.Id,ElementName=dgsample}" VerticalAlignment="Top" Width="100" />
<TextBox Name="tb2" HorizontalAlignment="Left" Height="20" Margin="60,60,0,0" TextWrapping="NoWrap" Text="{Binding SelectedItem.Name, ElementName=dgsample}" VerticalAlignment="Top" Width="100"/>
<TextBox Name="tb3" HorizontalAlignment="Left" Height="20" Margin="60,110,0,0" TextWrapping="NoWrap" Text="{Binding SelectedItem.Salary, ElementName=dgsample}" VerticalAlignment="Top" Width="100"/>

解决方案

You can additionally implement IDataErrorInfo as as follows in the view model. If you implement IDataErrorInfo, you can do the validation in that instead of the setter of a particular property, then whenever there is a error, return an error message so that the text box which has the error gets a red box around it, indicating an error.

class ViewModel : INotifyPropertyChanged, IDataErrorInfo
{
    private string m_Name = "Type Here";
    public ViewModel()
    {
    }

    public string Name
    {
        get
        {
            return m_Name;
        }
        set
        {
            if (m_Name != value)
            {
                m_Name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public string Error
    {
        get { return "...."; }
    }

    /// <summary>
    /// Will be called for each and every property when ever its value is changed
    /// </summary>
    /// <param name="columnName">Name of the property whose value is changed</param>
    /// <returns></returns>
    public string this[string columnName]
    {
        get 
        {
            return Validate(columnName);
        }
    }

    private string Validate(string propertyName)
    {
        // Return error message if there is error on else return empty or null string
        string validationMessage = string.Empty;
        switch (propertyName)
        {
            case "Name": // property name
                // TODO: Check validiation condition
                validationMessage = "Error";
                break;
        }

        return validationMessage;
    }
}

And you have to set ValidatesOnDataErrors=True in the XAML in order to invoke the methods of IDataErrorInfo as follows:

<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />

这篇关于WPF文本框验证C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆