我的验证不起作用 [英] my validation not working

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

问题描述

大家好

我正在研究需要验证用户输入的WPF应用程序。请帮我理解我错过的东西。以下是当前验证方案的示例:



PersonViewModel.cs:

  public   class  PersonViewModel:WorkspaceViewModel,IDataErrorInfo 
{

SDPS.Business.Base.BaseBusiness< Person> bz = new Business.Base.BaseBusiness< Person>();


public PersonViewModel( string displayTitle)
{
Items = bz.GetAll();
DisplayName = displayTitle;
NewCommand = new RelayCommand(p = > NewItem());
SaveCommand = new RelayCommand(p = > SaveItem(),s => CanSave );

}

#endregion

< span class =code-region>#region Methods
private void NewItem()
{

CurrentItem = new Person();


}
私人 无效 SaveItem()
{

if this .Error = = null
{
bz.Add(CurrentItem);
Items.Add(CurrentItem);
}
}

#region属性

public Person CurrentItem
{
get { return _CurrentItem; }
set
{
_CurrentItem = value ;
if value != null
{
_CurrentItem = value ;
}
OnPropertyChanged( CurrentItem);
}
}

public ObservableCollection< Person>商品
{
获取 {返回 _items; }
set
{
_items = value ;
OnPropertyChanged( Items);
}
}

#region变量
私人 ObservableCollection< Person> _items;
private Person _CurrentItem;

#endregion

public string 错误
{
get { return null ; }
}

public string this [ string propertyName]
{
get
{
if (propertyName == 名称
{

if (CurrentItem.Name == 0
return 错误......;
}
return ;
}
}





XAML使用验证:

 <   TextBox    名称  =  txtName    

高度 = 23 Horizo​​ntalAlignment = 保证金 = 53,52,0,0 VerticalAlignment = Top 宽度 = 120 >
< TextBox.Text >
< 绑定 路径 = CurrentItem.Name

ValidatesOnExceptions = True

UpdateSourceTrigger = PropertyChanged >

< / Binding >
< / TextBox.Text >
< / TextBox >

解决方案

Hi guys
I'm working on a WPF app which needs to validate user input. Please help me understand what I am missing. Below is an example of the current Validation scenario:

PersonViewModel.cs:

 public class PersonViewModel : WorkspaceViewModel, IDataErrorInfo
    {

        SDPS.Business.Base.BaseBusiness<Person> bz = new Business.Base.BaseBusiness<Person>();

    
        public PersonViewModel(string displayTitle)
        {
            Items = bz.GetAll();
            DisplayName = displayTitle;
            NewCommand = new RelayCommand(p => NewItem());
            SaveCommand = new RelayCommand(p => SaveItem(),s=>CanSave);
     
        }

        #endregion

        #region Methods
        private void NewItem()
        {
 
            CurrentItem = new Person();


        }
         private void SaveItem()
        {
 
            if (this.Error == null)
            {
                bz.Add(CurrentItem);
                Items.Add(CurrentItem);
            }
        }
#region Properties

        public Person CurrentItem
        {
            get { return _CurrentItem; }
            set
            {
                _CurrentItem = value;
                if (value != null)
                {
                     _CurrentItem = value;
                }
                OnPropertyChanged("CurrentItem");
            }
        }
         
        public ObservableCollection<Person> Items
        {
            get { return _items; }
            set
            {
                _items = value;
                OnPropertyChanged("Items");
            }
        }

        #region Variables
        private ObservableCollection<Person> _items;
        private Person _CurrentItem;
         
        #endregion
 
        public string Error
        {
            get { return null; }
        }

        public string this[string propertyName]
        {
            get
            {
                if (propertyName == "Name")
                {

                    if  (CurrentItem.Name == "0")  
                        return "Error...";
                }
                return "";
            }
        }



XAML Usage Validation:

<TextBox Name="txtName" 

                                 Height="23" HorizontalAlignment="Left" Margin="56,42,0,0"  VerticalAlignment="Top" Width="120"   >
                            <TextBox.Text>
                            <Binding Path="CurrentItem.Name" 

                 ValidatesOnExceptions="True"

                 UpdateSourceTrigger="PropertyChanged">
                                     
                                </Binding>
                            </TextBox.Text>
                        </TextBox>

解决方案

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

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