MVVM methodology of WPF for problem specified. I have a combo box say in a ‘MainView’ of XAML. Its code partner is ‘MainViewModel’ and exposes a property of a ‘Person’ which is basically just a seperate class(POCO class) for exposing a string and an int to represent a name and a seed in the database. It sets up a property of ReadOnlyCollection that binds to a combobox like so: (referencing the viewmodel at the top of the xaml like: xmlns:vm=”clr-namespace:(mylocationforviewmodelnamespace))
ItemsSource="{Binding Path=People}"
DisplayMemberPath="FirstName"
SelectedValuePath="PersonId"
This works great but I am then setting up a User Control View and it’s respective View Model code. What I am not getting about the ViewModel method of binding is how you bind the passed in values for the constructor? Or can you even do that? Or should I be setting up an intermediary class not just for my ‘model’ but for the ‘DataAccess’ ?
My end goal is to select a value in a combobox, which is already bound properly and works great, and pass it to the viewmodel code that then associates with the view when built and docks in the parent form. I can make a constructor just fine and set a static value to make a name appear when it builds. I don’t know how you pass the value from the parent view object of a combobox which is bound to the resulting creation of usercontrol. I am up for doing lots of things but I really want to stick to the MVVM method and not do this in code behind of which I already know how to do this.
MVVM method I am following loosely is here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
Okay I got it. The MainViewModel in the example contains private methods which are the constructors of the subsequent user controls. The main window binds to the combobox fine but needs more detail on the member of the combobox it needs to relate to a new constructor. This needs to be set and bound to a property bound to the element in XAML ‘selected value’ of the combobox. Once this property knows it is bound it can later on be used in an internal method in the ViewModelCode that is databound to inform the constructor of what the passed in Person object needs to be.
I see a lot of people with situations similar to mine but different so I thought I would post this if anyone may find it useful later. The only word I would add is that I believe you need to inherit “INotifyPropertyChanged” class but in my example it is in an abstract class two classes inheritance levels down so I felt it was not necessary to redo everything to show a simpler example as I got what I needed.
XAML:
Fields:
Constructor:
Helper Methods:
Constructor in MainViewModel of CHILD View Model: