I was hoping someone could help me get this simple WPF MVVM example off the ground as I am struggling to plumb the data into the view model.
I have an SQL Table Temperatures where each record has a time stamp and then a numerical value eg.
LogTime--------------Temperature
01/01/2013 00:00 --60
01/01/2013 00:05 --61.1
01/01/2013 00:10 --61.2
My WPF View would have a start datetime and end datetime picker and a gridview allow the user to select records from a date range and then display them in the grid
My Model is entity framework so where I am struggling is what would I need for the view model and where would the linq query go to pass the user entered start and end datetime. I am evetually going to chart the data and there is a definite requirment (scichart) to use an Observable Collection for the data. But I just wanted to get a very simplisitic data example to see how the database data is obtained in the ViewModel as an Observable Collection for binding to the view. I am also using Telerik WPF controls for my GridView and I know they may do things differently.
As you can tell I am a complete beginner and have struggled to find a simple example anywhere (loads of complex ones) else so any help is greatly appreciated.
This is actually quite a large subject. However, to keep things simple, and exclude design patterns and MVVM Frameworks for now...
You would need to create;
A WPF XAML View having:
A VieModel Class called something like clsMyTemperatureViewModel:
Here I've mocked up a small class to replicate your EF Context. You would however need to reference your EF Context rather than my _ListOfTemperatures collection. This would be something like;
The ViewModel basically exposes the relevant Public Properties to the View. One important thing to notice, is that you must implement the INotifyPropertyChanged interface, and Raise the PropertyChanged event for the view to update when the Properties have changed within the ViewModel.
You then need to add the following to your code behind New Sub;
This will set the DataContext of your View to the new ViewModel.
As I mentioned previously, this example doesn't attempt to involve any MVVM Frameworks, nor use any proper Design Patterns.
You should really be using the Repository Pattern for your data. It's within this Repository that you would place your Linq to Entities code, returning only an ObservableCollection to your ViewModel.
You would create a Solution having;
However, I hope this get's you going!
Create a view-model and have it look something like this:
Now in your view, you'll need to bind to the properties and collection. Something like this:
I haven't tested this, but it should give you a push in the right direction.