Hi,
try following. It works without problems:
CodeBehind and MainWind0w and classes in my namespace WpfApp003
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
using WpfApp003;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for Window003.xaml
/// </summary>
public partial class Window003 : Window
{
public Window003()
{
InitializeComponent();
this.DataContext = ViewModel.Instance;
}
private void Add_Click(object sender, RoutedEventArgs e)
{
var addContactWindow = new Window003A();
ContactDetail contactData = new ContactDetail("", "", "", "", "");
addContactWindow.DataContext = contactData;
addContactWindow.ShowDialog();
if (addContactWindow.DataContext != null)
{
ViewModel.Instance.ContactDetails.Add(contactData);
ViewModel.Instance.OnPropertyChanged("View");
}
}
private void Edit_Click(object sender, RoutedEventArgs e)
{
if (ContactListView.SelectedItem != null)
{
ContactDetail contactData = ContactListView.SelectedItem as ContactDetail;
if (contactData == null)
{
return;
}
else
{
var addContactWindow = new Window003A();
contactData.Cache();
addContactWindow.DataContext = contactData;
addContactWindow.Owner = Application.Current.MainWindow;
addContactWindow.ShowDialog();
if (addContactWindow.DataContext == null) contactData.Restore();
}
}
}
private void Remove_Click(object sender, RoutedEventArgs e)
{
if (ContactListView.SelectedItem != null)
WpfApp003.ViewModel.Instance.ContactDetails.Remove(ContactListView.SelectedItem as ContactDetail);
}
}
}
namespace WpfApp003
{
public class ContactDetail : INotifyPropertyChanged
{
private string _Firstname;
private string _Lastname;
private string _Address;
private string _Phone;
private string _Email;
public ContactDetail() { }
public ContactDetail(string Firstname, string Lastname, string Address, string Phone, string Email)
{
_Firstname = Firstname;
_Lastname = Lastname;
_Address = Address;
_Phone = Phone;
_Email = Email;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
#region Properties Getters and Setters
public string Firstname
{
get { return _Firstname; }
set { _Firstname = value; OnPropertyChanged(); }
}
public string Lastname
{
get { return _Lastname; }
set { _Lastname = value; OnPropertyChanged(); }
}
public string Address
{
get { return _Address; }
set { _Address = value; OnPropertyChanged(); }
}
public string Phone
{
get { return _Phone; }
set { _Phone = value; OnPropertyChanged(); }
}
public string Email
{
get { return _Email; }
set { _Email = value; OnPropertyChanged(); }
}
#endregion Properties Getters and Setters
private string _Firstname_Cache;
private string _Lastname_Cache;
private string _Address_Cache;
private string _Phone_Cache;
private string _Email_Cache;
public void Cache()
{
_Firstname_Cache = _Firstname;
_Lastname_Cache = _Lastname;
_Address_Cache = _Address;
_Phone_Cache = _Phone;
_Email_Cache = _Email;
}
public void Restore()
{
Firstname = _Firstname_Cache;
Lastname = _Lastname_Cache;
Address = _Address_Cache;
Phone = _Phone_Cache;
Email = _Email_Cache;
}
}
public class ViewModel : INotifyPropertyChanged
{
private static ViewModel _instance;
static ViewModel()
{
_instance = new ViewModel();
_instance.ContactDetails = new ObservableCollection<ContactDetail>();
_instance.cvs.Source = _instance.ContactDetails;
}
public static ViewModel Instance { get => _instance; }
public ICollectionView View { get => cvs.View; }
private CollectionViewSource cvs = new CollectionViewSource();
public ObservableCollection<ContactDetail>? ContactDetails { get; set; }
public event PropertyChangedEventHandler? PropertyChanged;
internal void OnPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
Window003A in my demo is your AddContactWindow:
using System.Windows;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for Window003A.xaml
/// </summary>
public partial class Window003A : Window
{
public Window003A()
{
InitializeComponent();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
this.DataContext = null;
Close();
}
private void Save_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}