views:

290

answers:

2

I have a ListControl that is populated with an ObservableCollection. I've also got a "Add new item" text box on the usercontrol as well. When I enter text into the textbox and click "Add" the item goes through the appropriate logic and adds to my ObservableCollection, which my ListControl reflects immediately. So far, so good.

HOWEVER. That textbox is still populated with the item I just added... if I try to change or backspace that textbox, the item I just added reflects the change as well! How can I get the textbox to clear out after adding a new item? I'm using an MVVM approach, so my textbox is bound to {Binding Path=Object.Name}.

Any ideas?

A: 

Not enough detail to provide a proper answer, but I'll give it a shot. It appears you need to create a new "Object" (as in Path=Object.Name) after adding the previous one to the ObservableCollection. This means the TextBox will be bound to the new object instead of the old one, and you will be able to repeat this process.

Jerry Bullard
A: 

When the Add button is pushed, I would clone the object that is in the TextBox binding and add the clone to the ObservableCollection.

Then you can either leave the object bound to the TextBox and edit it without messing up the one that was added, or you can just clear the TextBox out by setting the object to null.

timothymcgrath
When you say Clone, what exactly do you mean? I can't just say BackupObject = CurrentObject;BusinessClass.Save(BackupObject);CurrentObject = null;The reason is that it seems my BackupObject is simply pointing to the same in-memory object that CurrentObject is pointing to. Am i not cloning the object correctly then?
Shafique
No, you need to actually create a new object with the same data as the original. This is usually done by implementing the ICloneable interface. So, if the object were a string with the variable name theString, you would add theString.Clone() to the ObservableCollection, which will create a completely new String object.
timothymcgrath