Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
 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:
XML
<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>
Posted
Updated 16-May-14 6:06am
v4
Comments
[no name] 16-May-14 12:28pm    
What you are really missing is a description of a problem. We can't see your screen to know what it is that you are seeing.
IT-discovery 16-May-14 13:17pm    
the problem is that the mentioned Textbox never check the validation at all.and validation never fire.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900