Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I working on WPF application using MVVM pattern. the question is : How to Check wether the Textboxes content are valid or not and by the way make a Save Button Disable until all textboxes fill by valid data. I googled more and more but in all scenario the answer was implementing IDataerrorInfo on the model but i using entity frameworks entity as model so can not do the implementation. please give me some advice or links . thanks in advance. Below my project description: the XAML:
XML
<TextBox Name="txtName"    Height="23" HorizontalAlignment="Left" Margin="56,42,0,0"  VerticalAlignment="Top" Width="120"  >
                           <TextBox.Style>
                               <Style TargetType="TextBox">
                                   <Style.Triggers>
                                       <Trigger Property="Validation.HasError" Value="true">
                                           <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
                                       </Trigger>
                                   </Style.Triggers>
                               </Style>
                           </TextBox.Style>

                           <Binding   Path="CurrentItem.Name" UpdateSourceTrigger="PropertyChanged">
                               <Binding.ValidationRules>
                                   <vm:NotNullValidationRule  ValidatesOnTargetUpdated="True"/>
                               </Binding.ValidationRules>
                           </Binding>
                       </TextBox>


And the PersonViewModel.cs:
C#
public class PersonViewModel : WorkspaceViewModel
{

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

    #region Constructor

    public PersonViewModel(string displayTitle)
    {
        Items = bz.GetAll();
        DisplayName = displayTitle;
        NewCommand = new RelayCommand(p => NewItem());
        SaveCommand = new RelayCommand(p => SaveItem());
        UpdateCommand = new RelayCommand(p => UpdateItem(), p => CanUpdateOrDelete);
        DeleteCommand = new RelayCommand(p => DeleteItem(), p => CanUpdateOrDelete);

    }



    #endregion

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

    private void SaveItem()
    {

            bz.Add(CurrentItem);
            Items.Add(CurrentItem);

    }
    private void DeleteItem()
    {
        bz.Delete(CurrentItem);
        Items.Remove(CurrentItem);

        OnPropertyChanged("Items");

        OnPropertyChanged("CurrentItem");

    }
    public void UpdateItem()
    {

        bz.Update(CurrentItem);
        OnPropertyChanged("Items");
    }
    #endregion

    #region Properties

    public Person CurrentItem
    {
        get { return _CurrentItem; }
        set
        {
            _CurrentItem = value;

            OnPropertyChanged("CurrentItem");
        }
    }

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

    #endregion

    #region Variables
    private ObservableCollection<Person> _items;
    private Person _CurrentItem;

    #endregion


}

and the ValidationRule class is as below:
C#
 public class NotNullValidationRule : ValidationRule
{

    public virtual bool HasError
    {
        get
        {
            return   null;
        }
    }
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        var result = new ValidationResult(true, null);

        if(value!=null)
        if (string.IsNullOrWhiteSpace( value.ToString()) )
        {
            result = new ValidationResult(false, "null string not allowed");
        }

        return result;
    }
}
Posted

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