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:
<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:
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:
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;
}
}