Home > Back-end >  Why isn't <ListVIewItem> displaying on <ListView> in my WPF & MVVM application?
Why isn't <ListVIewItem> displaying on <ListView> in my WPF & MVVM application?

Time:11-04

I am building a instant messaging app and I encuntered a problem where the isn't showing in binded . This is the I want to add in that I created in a "ContactCard" model. ListView

Basically everything worked right until I added a pop up window which opens when you click on drop down item "add" in "contacts" menu. AddContact window

I know that the problem lies within binding mechanism, because there are some biding errors displayed in the pop up window. Binding errors

Logic behind adding a new contact is that uppon pressing on "Add Contact" button in "AddContact" pop up window there is an event handler executed, which consists of the following code:

 public partial class AddContact : System.Windows.Window
    {
        public MainWindow mainWindow;
        public AddContact()
        {
            InitializeComponent();
            mainWindow = new MainWindow();
            mainWindow.DataContext = this;
        }
        
        int st = 1;
        private void AddNewContactBtn_Click(object sender, RoutedEventArgs e)
        {
            string username = this.contactUsername.Text;
            string imageUrl = this.contactImageURL.Text;

            ContactModel newContact = new ContactModel
            {
                Username = "Nixt",
                ImageSource = "https://www.dictionary.com/e/wp-content/uploads/2018/04/Sid-the-Sloth-300x300.jpg",
                Messages = new ObservableCollection<MessageModel>(new List<MessageModel> {

                    new MessageModel
                    {
                        FirstMessage = true,
                        ImageSource = $"https://www.dictionary.com/e/wp-content/uploads/2018/04/Sid-the-Sloth-300x300.jpg",
                        Message = $"Sporočilo {st   1}",
                        Username   = "Nixt",
                        UsernameColor = "Red"
                    } ,

                    new MessageModel
                    {
                        Username   = "Nixt",
                        UsernameColor = "Red",
                        FirstMessage = false,
                        ImageSource = "https://www.dictionary.com/e/wp-content/uploads/2018/04/Sid-the-Sloth-300x300.jpg",
                        Message = $"Sporočilo {st   2} ",
                        Time = DateTime.Now
                    },

                    new MessageModel
                    {
                        Username = "povodnikt",
                        UsernameColor = "#409AFF",
                        ImageSource = "https://imgur.com/FeMHSd6.jpg",
                        Message = $"Zdravo? {st   3}",
                        Time = DateTime.Now
                    },

                    new MessageModel
                    {
                        Username = "povodnikt",
                        UsernameColor = "#409AFF",
                        ImageSource = "https://imgur.com/FeMHSd6.jpg",
                        Message = $"Adijo frend! {st}",
                        Time = DateTime.Now
                    }
                })
            };

            mainWindow.AddNewContactToCollection(newContact);
        }
    }

AddNewContactToCollection function in MainWindow.xaml.cs

public void AddNewContactToCollection(ContactModel newContact)
        {
            viewModel2.Contacts.Add(newContact);
        }

It basically worked before without the pop up window, so I tried removing the window, but since I need it this is not a solution to my problem.

CodePudding user response:

You could inject the AddContact window with a reference to the existing instance of the MainWindow instead of creating a new instance:

private readonly  MainWindow mainWindow;

public AddContact(MainWindow mainWindow)
{
    InitializeComponent();
    this.mainWindow = mainWindow;
    ...
}

If you display the AddContact window from the MainWindow, you simply instantiate it like this:

AddContact window = new AddContact(this);
  • Related