views:

145

answers:

3

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

+1  A: 

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.

Tendlon
@Tendlon: Do you want him to add an empty employee object to the collection or the one created from the textbox data? 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.
Veer
@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
@Tendlon: How the data from the textbox is saved to the empty employee object then?
Veer
@Veer, databound to the SelectedEmployee property (or at least thats how I understood the text from the poster).
Tendlon
A: 

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. :-)

jbe
A: 

Do you wan't by default to jump into the Create new employee state? 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.

Ingó Vals