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 ""; } }
<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>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)