I am just starting out with WPF and MVVM. I know this question was probably asked a lot on SO but I have read everything I could find on MVVM and EF, tried every example and tutorial out there, read "How to survive MVVM for Enterprise" but I still cannot understand how to properly use the pattern and the framework for a simple bind to a textbox. Can someone please help me with an easy to understand example?
I have created a model using EF Designer. Inside my Model folder, the LocationModel.Context looks like this:
MODEL:
namespace Location.Model
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class DailyEntities : DbContext
{
public DailyEntities()
: base("name=DailyEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<LocationKPI> LocationKPI { get; set; }
}
}
LocationKPI.cs
namespace Location.Model
{
using System;
using System.Collections.Generic;
public partial class LocationKPI
{
public long sMonth { get; set; }
public Nullable<decimal> Efficiency { get; set; }
}
}
I've created a ViewModel folder with a class that implements INotifyPropertyChanged:
VIEWMODEL:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Location.ViewModel
{
public class LocationViewModel: INotifyPropertyChanged
{
public decimal Efficiency
{
get { return _Efficienecy; }
set
{
_Efficiency = value;
OnPropertyChanged("Efficiency");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
XAML:
<Window x:Class="Location.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Location"
xmlns:vm="clr-namespace:Location.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DatePicker HorizontalAlignment="Left" Margin="392,17,0,0" VerticalAlignment="Top"/>
<TextBlock Text="Efficiency"></TextBlock>
<TextBox Text="{Binding Efficiency}" ></TextBox>
</Grid>
</Window>
I am really losing my mind here. I am looking for some guidance on how to "glue" the model and the viewmodel to my view and I just need a simple example to understand. I am sorry if the question is too broad.
Is the query supposed to be written in the model or the viewmodel? How do I define the data context and write a simple select Efficiency where sMonth = 9 statement using linq? How do add the month from the datepicker as a parameter to the above query? I would be so grateful for any kind of help. Thank you.
modelorviewmodel, you would have aservicelayer that will do that, that could be whatever pattern you want to adopt on your application likeRepositorypattern orCQRS.