WPF binding to collection and adding new item to collection using MVVM?

I suggest that by clicking "Add Employee" a new employee object is created and assigned to SelectedEmployee.

Up vote 2 down vote favorite 1 share g+ share fb share tw.

This is probably a simple question... I have a window with a listbox of employees and a # of textboxes for displaying or entering data. The listbox of employees is bound to an observable collection in my ViewModel, which is read from a database. Right now I have a SelectedEmployee property that the textboxes are bound to, so if a user clicks on a existing employee it displays the existing attributes.

Now I want to add a new employee from values entered into the textboxes. My thought was a user would enter the values and then click "Add Employee". But I'm confused as to what the textboxes would be bound to.

I don't have an employee object yet and to display existing employees I have need to bind to the SelectedEmployee. I thought about changing the work flow where a user needs to click "Add New Employee" enter the data and click "Save" but I think I run into the same issue. I think I'm missing something obvious or my ViewModel is incorrect to support my work flow.

Any suggestions? Thanks wpf data-binding mvvm link|improve this question asked Jun 8 '10 at 18:16dark fader1,73741431 54% accept rate.

1 Actually using a "Add New Employee" and "Save" button I think would work but it seems like a simpler work flow of just a single click "Add" should work. – dark fader Jun 8 '10 at 18:36.

I suggest that by clicking "Add Employee" a new employee object is created and assigned to SelectedEmployee. Now that I think about it, maybe it would be better to create a employee object, add it to your collection and point SelectedEmployee to the newly created object.

The latter will need him to access the UI object properties by name or atleast to get the update using explicit trigger which in turn requires the UI object for binding expression. The former makes no sense at all. – Amsakanna Jun 10 '10 at 13:16 @Veer: I was thinking of adding an empty employee object to the collection, and set SelectedEmployee to the new item in the collection.

Why doesn't it make sense? Apologies if I missunderstand something obvious, english is not my native language. – Tendlon Jun 10 '10 at 14:09 How the data from the textbox is saved to the empty employee object then?

– Amsakanna Jun 10 '10 at 14:22 @Veer, databound to the SelectedEmployee property (or at least thats how I understood the text from the poster). – Tendlon Jun 10 '10 at 17:18.

You might find the BookLibrary sample application of the WPF Application Framework (WAF) useful. It is very similar to the application you describe but it uses books instead of employees. :-).

If you wan't to press some button first here is something that might help. You could put the Collection into a IEditableCollection let the Employee object implement IEditableObject. Then the user would press a button wich calls the addnew() method of the IeditableCollection (let's call it IEC) wich would add a new item to the collection wich would be in edit mode.

IEC. CancelNew would remove the object again from the collection IEC. CommitNew would add the object permanently and stop editing it.

You would also have access to EditItem, CommitEdit and CancelEdit to edit a object already present. This removes the hassle of having to save the object when it still hasn't got the right info on it first and then edit the right info in.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions