I am working on a WPF database application using the MVVM-Light Framework. I use SimpleIOC to enter my data services, and my ViewModels connect to the view from the XAML file. However, when opening new view models, I usually also need to pass another object to the view model (let's say I pass an integer CarId so that I can capture the actual car from the database).
Now I pass this using Messenger in the view, after InitializeComponent (). Then it falls into the view model. This currently works, however it has a few deviations.
First, a message is not sent until the View model constructor is called. Suppose I had a property of the ThisCar object in the ViewModel, and I had secondary properties, such as a string called Brand, which returns ThisCar.Brand.
public const string ThisPersonPropertyName = "ThisCar"; private Model.Car _thisCar; public Model.Car ThisCar { get { return _thisCar; } set { if (_thisCar== value) { return; } RaisePropertyChanging(ThisCarPropertyName); _thisCar= value; RaisePropertyChanged(ThisCarPropertyName); } } public const string BrandPropertyName = "Brand"; public string Brand { get { return ThisCar.Brand; } set { if (ThisCar.Brand == value) { return; } RaisePropertyChanging(BrandPropertyName); ThisCar.Brand = value; RaisePropertyChanged(BrandPropertyName); } }
Now that the constructor receives the call, I have to initialize ThisCar in the View Model Constructor. So otherwise, I get a null error on ThisCar.Brand when creating the ViewModel.
ThisCar = new CarObject();
Then, after initializing the empty object, the message will be called, and I will pull the real car from the database based on the identifier of the car.
ThisCar = webService.GetCar(carId);
However, when I do this, the Brand property is not updated because nothing calls OnPropertyChanged for the brand.
I have two works, but I like them more. First on manually calls OnPropertyChanged for any properties that need to be updated. Secondly, I remove all the small properties and bind everything directly to the ThisCar property (this means that I have to implement INotifyPropertyChanged in my model).
Please let me know if I'm just picky, or if there is a better way to do this. The way I do this works, but just doesn't sit right with me. Thanks.