I have two instances of a File Browser user control, the control is just a button and a text box, but I need two instances, think a diff utility. Once the user has selected a file for each control I want to enable a button which will perform an action on both of the files.
The problem I am having is how to distinguish between the instances of the controls in order to determine that both files have been selected. I think I would like for my DoSumthinViewModel to only have string properties which the FileChooserViewModel fulfills.
At first I had a single ViewModelLocator with a property which returned a new instance of a the FileChooserVM when accessed, but this just didn’t seem right and I could not distinguish between the instances. I then went down the path of a separate Locator for the FileChooser but realized that each control would be talking to the same locator instance and thus the same FileChooserViewModel again.
So, what would be a good technique for working with individual instances of the same ViewModel?
Thanks,
Shane Holder
Your DoSomethingViewModel could have two properties of type FileChooserViewModel that your controls are bound to, then check their string properties for a value.
A simplified version of your FileChooserViewModel could be…
And your DoSomethingViewModel might look like this…
The NotificationMessageReceived method is called with a NotificationMessage is sent from FileChooserViewModel’s FilePath property setter, and it in turn raises the property changed event on the BothFilesChosen property.
Another way to do this would be to handle the PropertyChanged event on each FileChooserViewModel property, but I prefer using messaging because event handling means you need to make sure you un-handle the events, and this can get messy leading to memory issues when events are missed.